Go Back   Dynamic Drive Forums > General Coding > PHP
Search Dynamic Drive Forums:

Reply
 
Thread Tools Search this Thread
  #1  
Old 11-07-2007, 05:39 PM
Jas's Avatar
Jas Jas is offline
Senior Coders
 
Join Date: Jan 2007
Posts: 629
Thanks: 10
Thanked 28 Times in 28 Posts
Post The FTP Script is Here

(Don't know if this is the right place for this, but. . . )

At long last, here is my version of FTP Uploading with PHP.
FEATURES: Uploads content, saves info in a table, zip's EXE files, uploads only filetypes you deem safe, simple anti-overwrite feature . . .
TO SET UP:

FILE: form.php
CONTENTS:
Code:
<?php
function showform(){
	if($_GET['error'] == 1){
		$error = '<h3><u>ERROR:</u> You must put a title.</h3>';
	}elseif($_GET['error'] == 2){
		$error = '<h3><u>ERROR:</u> That file format is not supported.</h3>';
	}elseif($_GET['error'] == 3){
		$error = '<h3><u>ERROR:</u> Your file failed to load.</h3>';
	}elseif($_GET['error'] == 'x'){
		$error = '<h3><u>UPLOAD:</u> Your file has been uploaded to the server.</h3>';
	}

	print "    <html>
<head>
    <style type='text/css'>
    	body{
    		text-align: center;
		}div#left, div#right{
			float: left;
			width: 300px;
			height: 300px;
		}div#base{
		 	float: left;
			width: 600px;
			border-style: solid;
			border-color: #000000;
			border-width: 1 0 1 0;
			text-align: center;
		}p{
			text-align: justify;
			text-indent: 30px;
			margin: 0;
		}div#terms{
			width: 600px;
			margin-right: auto;
			margin-left: auto;
		}div#form{ width: 600px; display: none; text-align: left; margin: auto; }
	</style>
	<script type='text/javascript'>
	<!--
		function agree(){
			document.getElementById('terms').style.display = 'none';
			document.getElementById('form').style.display = 'block';
		}
	//-->
	</script>
</head>
<body>
    <div id='terms'>
<!--========================================//-->
		<div style='width:600px; margin:auto; font-size:1px;'>$error</div>
        <h2><p style='text-align: center'>TERMS OF USE:</p></h2>
        <hr></hr>
        <p>YOUR TERMS OF USE GO HERE</p>
        <p style='text-align: center'>I agree:<input type='radio' onclick=agree() name='1'>
		I disagree:<input type='radio' onclick='javascript:history.back()' name='1'></p>
<!--========================================//-->
    </div>
    <div id='form'>
        <form name='upload' enctype='multipart/form-data' action='upload.php' method='POST'>
            <h2>Submit Information</h2>
		    <div id='left'>
                 File To Upload:<br>
                <input type='file' name='up_file'><br>
                 Title:<br>
                <input type='text' name='title'><br>
                 Description:<br>
                <textarea name='discription'></textarea><br>
            </div>
            <div id='right'>
                Notes:<br>
                <textarea name='notes'></textarea><br>
                 Copyright:<br>
                <input type='text' name='copyright'><br>
            </div>
            <div id='base'>
                <input type='submit' name='Submit' value='Submit'>
                <input type='reset' value='Reset'>
                
                <input type=hidden name=box value=''>
                
            </div>
        </form>
    </div>
Script by: Jason Mace 2007
</body>
    </html>";
}
?>
FILE: upload.php
(NOTE THAT THIS IS THE FILE THAT SHOULD BE VIEWED)
CONTENTS:
Code:
<?php

	$Connection = MySQL_Connect($host, $name, $pass) or die ('NO CONNECTION TO MySQL');

if (!MySQL_Select_db('Files')){
print 'Create DB: '. mysql_query('CREATE DATABASE Files');
print '<br>Select DB: '.MySQL_Select_db('Files');
print '<br>Create Table: '. 

MySQL_query("CREATE TABLE files (
	num SERIAL,
	path VARCHAR(150),
	title VARCHAR(100),
	filetype VARCHAR(20),
	disc VARCHAR(500),
	provider VARCHAR(50),
	notes VARCHAR(500),
	copyright VARCHAR(150)
	);");

}else{}

if ($_POST['Submit'] == 'Submit'){
	if($_POST['title'] == ''){
		header("Location: ./upload.php?error=1");
	}else{
		set_time_limit(0);
	
		$conn_id = ftp_connect($host) or die("Couldn't connect");
		$login_result = ftp_login($conn_id, $ftpName, $ftpPass) or die("ERROR IN FTP CONNECTION");
		
		
		$theFile = $_FILES['up_file'];
		$source = $theFile['tmp_name'];
		$file_destination = $_FILES['up_file']['name'];
	
		$allowedTYPES = array('JPEG','JPG','AVI','MPEG','WMV','PDF','DOC','DOCX','TXT','EXE','ZIP');
		$filetype = pathinfo($_FILES['up_file']['name']);
		$filetype = strtoupper($filetype['extension']);
	
		if(array_search($filetype, $allowedTYPES)){	
			ftp_chdir($conn_id, "/information/");
			ftp_chdir($conn_id, "upload");
			$path = './information/upload/';
	
			$i = 0;
			while(is_file('./upload/'.$file_destination) == 1){
				$file_destination = $i.$file_destination;
				$i++;
			}

			$path2= '/upload/'.$file_destination;
	
			$upload = ftp_put($conn_id, $file_destination, $source, FTP_BINARY);
			
			if($filetype == 'EXE'){
				$zip = new ZipArchive;
				$res = $zip->open("./upload/$file_destination.zip", ZipArchive::CREATE);
				if ($res === TRUE) {
				    $zip->addFile("./upload/$file_destination", $file_destination);
				    $zip->close();
				    unlink("./upload/$file_destination");
				    $path2 .= '.zip';
				} else {
				    $upload = false;
				}
			}else{}
			
			if($upload){
		
				$title 		= $_POST['title'];
				$disc		= $_POST['discription'];
				$provider	= $_SESSION['username'];
				$notes		= $_POST['notes'];
				$copyright	= $_POST['copyright'];
				
				MySQL_Query("INSERT INTO files VALUES(
					NULL,
					'$path2',
					'$title',
					'$filetype',
					'$style',
					'$disc',
					'$sugrank',
					'$provider',
					'$notes',
					'$copyright'
				)");
				
				header("Location: ./upload.php?error=x");
		
			}else{ header("Location: ./upload.php?error=3"); }
		}else{ header("Location: ./upload.php?error=2&filetype=$filetype");}
		MySQL_Close($Connection);
		ftp_close($conn_id);
	}
} else {
	showform();
}

?>
There is a little more work to set it up. These files should be in a folder called "information", and you need to create a folder inside "information" called "upload". Make sure that you have the PHP_zip option enabled in your ini file. The fastest way to do that is to click the wamp server quicklink on the taskbar>> PHP Settings >> Extensions >> PHP_zip. You will also need to download and set up an FTP Server as well. This is VERY easy to do. I am using FileZilla Server. Lastly, you also need to put this line of code into your .htaccess file:
Code:
php_value upload_max_filesize 50M
This controls the max filesize your script will allow. Note that if you do not add this to your .htaccess, it will only DL things as big as JPEG files.

*All the things in red should be changed.

If you have any questions, feel free to post them. And, I did spend several months creating this, so an "Upload Script by Jason Mace" somewhere on the site would be great-- You don't have to, of course. . .
__________________
--Jas
function GreatMinds(){ return "Think Like Jas"; }
I'm gone for a while, but in the meantime: Try using my FTP script | Fight Bot Form Submissions

Last edited by Jas; 11-08-2007 at 09:42 PM.
Reply With Quote
  #2  
Old 11-07-2007, 07:35 PM
pssparkman pssparkman is offline
Junior Coders
 
Join Date: Oct 2007
Posts: 52
Thanks: 0
Thanked 0 Times in 0 Posts
Default

So if I understand this right I can upload large video files right? Up to how big would you say?

Also, what is it asking for when it says $conn_id above in your upload.php code? Also, where would the .htaccess file be in the ftp server? One last thing, I have copied and pasted the two portions of code and tried to bring up the form and nothing comes up. What could be the problem?

Last edited by pssparkman; 11-07-2007 at 10:25 PM.
Reply With Quote
  #3  
Old 11-08-2007, 12:21 AM
Jas's Avatar
Jas Jas is offline
Senior Coders
 
Join Date: Jan 2007
Posts: 629
Thanks: 10
Thanked 28 Times in 28 Posts
Default

Quote:
Originally Posted by pssparkman View Post
So if I understand this right I can upload large video files right? Up to how big would you say?
Code:
php_value upload_max_filesize 50M
This sets it to 50 MB, which is very high. You probably want to lower it.

Quote:
Originally Posted by pssparkman View Post
Also, what is it asking for when it says $conn_id above in your upload.php code?
that is the connection to FTP.

Quote:
Originally Posted by pssparkman View Post
Also, where would the .htaccess file be in the ftp server? One last thing, I have copied and pasted the two portions of code and tried to bring up the form and nothing comes up. What could be the problem?
.htaccess is a file on the server (named .htaccess) that contains settings for your webserver--it's not part of the FTP server. If you don't have one on your webserver yet, create one. Put the code into the file and name it .htacces and put it in the root folder. As for the last question, are you viewing the upload.php file? (Not the form.php file.)
__________________
--Jas
function GreatMinds(){ return "Think Like Jas"; }
I'm gone for a while, but in the meantime: Try using my FTP script | Fight Bot Form Submissions

Last edited by Jas; 11-08-2007 at 12:28 AM.
Reply With Quote
  #4  
Old 11-08-2007, 12:41 AM
pssparkman pssparkman is offline
Junior Coders
 
Join Date: Oct 2007
Posts: 52
Thanks: 0
Thanked 0 Times in 0 Posts
Default

I went to view the form.php and it displayed nothing on the screen. I copied and pasted the form.php code exactly as you had it and then went to view it in the browser and nothing came up.

As for the $conn_id do I need to replace that with anything. I still don't understand. Sorry, as my coding skills just simply suck.

The 50MB file size is actually too small for the video files that I need to uploaded. Will it handle say up to 100-200MB files?

Thanks.
Reply With Quote
  #5  
Old 11-08-2007, 04:47 AM
Jas's Avatar
Jas Jas is offline
Senior Coders
 
Join Date: Jan 2007
Posts: 629
Thanks: 10
Thanked 28 Times in 28 Posts
Default

Quote:
Originally Posted by pssparkman View Post
I went to view the form.php and it displayed nothing on the screen. I copied and pasted the form.php code exactly as you had it and then went to view it in the browser and nothing came up.
View the upload.php file NOT the form.php file. The form.php file contains only a function to display the form; the upload.php file calls it. I should have made that clearer--sorry. View the upload.php file.

Quote:
Originally Posted by pssparkman View Post
As for the $conn_id do I need to replace that with anything. I still don't understand. Sorry, as my coding skills just simply suck.
Sorry! That should not have been in bold/red. That part does not need editing-- my mistake.

Quote:
Originally Posted by pssparkman View Post
The 50MB file size is actually too small for the video files that I need to uploaded. Will it handle say up to 100-200MB files?
That *might* be too big to do-- I haven't tried. I would suggest condencing the files to make them smaller, because you probably don't want files that big being sent to your server-- it'll fill up really really fast. But the script might take it if you edit the 50M to something larger, like the 200M you want. Let me know.

Did you figure out everything else okay?
__________________
--Jas
function GreatMinds(){ return "Think Like Jas"; }
I'm gone for a while, but in the meantime: Try using my FTP script | Fight Bot Form Submissions

Last edited by Jas; 11-08-2007 at 05:04 AM.
Reply With Quote
  #6  
Old 11-08-2007, 05:11 AM
pssparkman pssparkman is offline
Junior Coders
 
Join Date: Oct 2007
Posts: 52
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Yeah, everything is pretty explanatory.


When trying to view it, this came up:
Code:
Parse error: parse error, unexpected ')', expecting '(' in .........
The line in question is:
Code:
$res = $zip->open("./uploads/$file_destination.zip", ZipArchive::CREATE);

Last edited by pssparkman; 11-08-2007 at 07:21 AM.
Reply With Quote
  #7  
Old 11-08-2007, 09:33 PM
Jas's Avatar
Jas Jas is offline
Senior Coders
 
Join Date: Jan 2007
Posts: 629
Thanks: 10
Thanked 28 Times in 28 Posts
Default

It might be that you need to enable that feature in you PHP ini file. The easiest way to do that is to click the wamp server quicklink on the taskbar>> PHP Settings >> Extensions >> PHP_zip

EDIT: There was also an error in the file location. Try replacing the zip portion of code as well (I fixed it).
EDIT2: Actually, replace the whole file. There was another location error for the MySQL area.

Try enabing that, replacing the code, and If it doesn't work, give me the full error message and I'll see what I can do from there.
__________________
--Jas
function GreatMinds(){ return "Think Like Jas"; }
I'm gone for a while, but in the meantime: Try using my FTP script | Fight Bot Form Submissions

Last edited by Jas; 11-08-2007 at 09:43 PM.
Reply With Quote
  #8  
Old 11-12-2007, 08:08 AM
insanemonkey insanemonkey is offline
Regular Coders
 
Join Date: Oct 2005
Posts: 255
Thanks: 1
Thanked 0 Times in 0 Posts
Default

for somereason, i get a 500 internal error when i put the .htaccess in my file, why is this, i dont understand it..
__________________
Hey new design new look, goto xudas for personal webdsign help.. (:
Reply With Quote
  #9  
Old 11-12-2007, 07:01 PM
pssparkman pssparkman is offline
Junior Coders
 
Join Date: Oct 2007
Posts: 52
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Jas, what file type does the .htaccess have to be? (htm, php, html, etc.)
Reply With Quote
  #10  
Old 11-13-2007, 05:44 AM
insanemonkey insanemonkey is offline
Regular Coders
 
Join Date: Oct 2005
Posts: 255
Thanks: 1
Thanked 0 Times in 0 Posts
Default

.htaccess is its own file..
put that htaccess code into a notepad.
save as
name=
.htaccess
and make sure it says .htaccess only..
it should not say .htaccess.txt
or anything just .htaccess...
__________________
Hey new design new look, goto xudas for personal webdsign help.. (:
Reply With Quote
Reply

Bookmarks

Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT. The time now is 06:56 PM.

Home - Contact Us - Archives - Link to DD - Top 

Powered by vBulletin® Version 3.8.1
Copyright ©2000 - 2010, Jelsoft Enterprises Ltd.