GIDForums  

Go Back   GIDForums > Computer Programming Forums > MySQL / PHP Forum
User Name
Password
Register FAQ Members List Calendar Search Today's Posts Mark Forums Read

 
 
Thread Tools Search this Thread Rate Thread
  #1  
Old 18-Oct-2004, 23:14
da_bomb50 da_bomb50 is offline
New Member
 
Join Date: Jul 2004
Posts: 21
da_bomb50 is on a distinguished road

Upload Script


I am looking for a script to upload a file to the server using http. It will be pictures in particular.

<input type="file"> <= This is where the filename will be
i need a script to get this file on a local computer and upload it to the host.
  #2  
Old 18-Oct-2004, 23:16
BobbyDouglas's Avatar
BobbyDouglas BobbyDouglas is offline
Regular Member
 
Join Date: Aug 2003
Posts: 789
BobbyDouglas has a spectacular aura aboutBobbyDouglas has a spectacular aura about
I would check out www.hotscripts.com
__________________
Mr. Bob's Web Design - Tirelessly looking for ways to enhance the customer base of your business.
  #3  
Old 19-Oct-2004, 11:10
JasonMichael's Avatar
JasonMichael JasonMichael is offline
Awaiting Email Confirmation
 
Join Date: Jul 2004
Posts: 135
JasonMichael has a spectacular aura about
Cool

Why go any further, when you can get your help for uploading a file with PHP, right here!

The HTML Image Upload Form

The first thing you need, in order to upload a file, is an HTML page that has a a FORM with enctype="multipart/form-data", an input tag of type="file", and an input of type="submit" (of course).

Consider the following HTML form to upload the file:

HTML Code:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html> <head> <title>Upload an Image With PHP</title> </head> <body> <form method="post" enctype="multipart/form-data" action="upload.php"> Your Image file: <input type="file" name="filename"> <input type="submit"> </form> </body> </html>

Some Initial Considerations

The key thing to consider, before using this script is where your destination files will be kept. You will need to make sure that the directory you set the IMAGES_PATH constant to has proper permissions to be writable by your scripts. You may have to set this directory to have permissions set to '777'. Also, if you have errors due to an initial default limit, set during your installation of PHP, you can fix this by having the following setting in a .htaccess file, where your script resides:

This will make sure you have practically no limit on filesize. To limit the filesize, lower the number listed below, or limit it within your PHP script by checking the $_FILES:['your_file_input_field_name_that_you_use_in_your_H TML_code']['size'] value.

Code:
php_value upload_max_filesize 999999999

You can also set where you want temporary files saved, by using the following setting in your .htaccess file. I had to use this once when a hosting provider made the servers tmp directory unwritable by my scripts.

Code:
php_value upload_tmp_dir /usr/local/apache/htdocs/mytmpimages/

The Image Upload PHP Script

The following script may be used to upload a file, using PHP. I've incorporated some simple error handling.

PHP Code:

define ('IMAGES_PATH',"[DESTINATION PATH TO YOUR IMAGES THIS SCRIPT UPLOADS]"); // make sure that your scripts have write privileges to this path!!!
              $filename = $_FILES['filename']['name'];
        $tmp_filename = $_FILES['filename']['tmp_name'];
                

                

                if ($tmp_filename) {  
                
                     // test to make sure the file is of the correct file type
                       $filename_array = explode(".",$filename);
                     $type= $filename_array[1];
                     if ($type!="gif" & $type !="jpg" & $type!="png") {
                             die ("File type not recognized as a .gif, .jpg, or .png");
                     } else {
                     
                          $newfile = IMAGES_PATH.$filename; // setup destination filename
                         if (!file_exists($newfile)) {
                                                  @$result = move_uploaded_file($tmp_filename,$newfile); // @ used to suppress error messages
                                                 // move_uploaded_file also has a security feature built in to only move files that are HTTP posted uploads.
                                                       
                              if (!$result) {
                                      die ("Unable to move file.");
                              } else {
                                   echo "Congradulations: Your file '$newfile' was uploaded!";              
                             } // end if - !$result 
                         
                         } else {
                              die( "Sorry, but that file '$newfile' already exists.");                              
                         } // end if - file_exists
                 } // end if - $type
                     
                     
                     
                } else {

                     die("No Filename Provided.");
                } // end if - $tmp_filename 



Concluding Thoughts For Our PHP Image Upload Script

In order to understand how this works, you should understand that when an HTML form is posted of the type we used in this example, an input field of type="FILE" will tell the HTML server that a file is to be posted to the server's memory and transfered to the harddrive in its TMP directory. PHP allows for you to access details of the transfer by using the $_FILE server array variable.

The $_FILE array variable has the following key fields:
name
type
tmp_name
error
size

For details of your file transfer, you could always list the elements of the $_FILE array by using the print_r($_FILE) command.

Once the attempt to transfer the file to the harddrive of the server, our script checks to see if the $_FILE['name'] is set in the $filename, and then does the processing to check if the filename is of a recognized type. We could have checked this using the $_FILE array, but checking the filename, itself, ensures that your users are uploading files with proper suffixes.

I hope that this helps explain some of the mystery behind the process of uploading a file using PHP. This tutorial only covers the very basic process in order to explain it to a beginner. A more advanced example would illustrate the usage of a PHP Image Upload class, and offer the capability of storing filenames in a database or listing the directory in a graphical format. The possibilities are endless.
  #4  
Old 20-Oct-2004, 03:28
Darksat Darksat is offline
Awaiting Email Confirmation
 
Join Date: Aug 2004
Location: London
Posts: 97
Darksat is on a distinguished road
It wont overwrite old files so?
  #5  
Old 20-Oct-2004, 10:10
JasonMichael's Avatar
JasonMichael JasonMichael is offline
Awaiting Email Confirmation
 
Join Date: Jul 2004
Posts: 135
JasonMichael has a spectacular aura about
You could probably modify the script to ask if you want to overwrite a file, before overwriting. Otherwise, when you use move_uploaded_file, the file will get overwritten.
  #6  
Old 25-Oct-2004, 23:04
da_bomb50 da_bomb50 is offline
New Member
 
Join Date: Jul 2004
Posts: 21
da_bomb50 is on a distinguished road
This helps a lot, thanks.
  #7  
Old 21-Mar-2005, 11:01
Driven Driven is offline
New Member
 
Join Date: Mar 2005
Posts: 1
Driven is on a distinguished road
I know this thread is really old so I'm sorry for digging it up again but it deals with almost exactly what I need.

JasonMichael, could you expand on the above script for me a little bit more? What I need is to rename the image with the last row id (auto_increment) of my db table "properties" and then store the file name in the "mainphoto" column of that row.

So if the last row id in my db is 10 and someone uploads mypic.gif then it would be uploaded as 10.gif and the value "10.gif" would be stored in the "mainimage" field of row id 10.

Hope that makes sense. Thanks in advance for your help.

Quote:
Originally Posted by JasonMichael
Why go any further, when you can get your help for uploading a file with PHP, right here!

The HTML Image Upload Form

The first thing you need, in order to upload a file, is an HTML page that has a a FORM with enctype="multipart/form-data", an input tag of type="file", and an input of type="submit" (of course).

Consider the following HTML form to upload the file:

HTML Code:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html> <head> <title>Upload an Image With PHP</title> </head> <body> <form method="post" enctype="multipart/form-data" action="upload.php"> Your Image file: <input type="file" name="filename"> <input type="submit"> </form> </body> </html>

Some Initial Considerations

The key thing to consider, before using this script is where your destination files will be kept. You will need to make sure that the directory you set the IMAGES_PATH constant to has proper permissions to be writable by your scripts. You may have to set this directory to have permissions set to '777'. Also, if you have errors due to an initial default limit, set during your installation of PHP, you can fix this by having the following setting in a .htaccess file, where your script resides:

This will make sure you have practically no limit on filesize. To limit the filesize, lower the number listed below, or limit it within your PHP script by checking the $_FILES:['your_file_input_field_name_that_you_use_in_your_H TML_code']['size'] value.

Code:
php_value upload_max_filesize 999999999

You can also set where you want temporary files saved, by using the following setting in your .htaccess file. I had to use this once when a hosting provider made the servers tmp directory unwritable by my scripts.

Code:
php_value upload_tmp_dir /usr/local/apache/htdocs/mytmpimages/

The Image Upload PHP Script

The following script may be used to upload a file, using PHP. I've incorporated some simple error handling.

PHP Code:

define ('IMAGES_PATH',"[DESTINATION PATH TO YOUR IMAGES THIS SCRIPT UPLOADS]"); // make sure that your scripts have write privileges to this path!!!
              $filename = $_FILES['filename']['name'];
        $tmp_filename = $_FILES['filename']['tmp_name'];
                

                

                if ($tmp_filename) {  
                
                     // test to make sure the file is of the correct file type
                       $filename_array = explode(".",$filename);
                     $type= $filename_array[1];
                     if ($type!="gif" & $type !="jpg" & $type!="png") {
                             die ("File type not recognized as a .gif, .jpg, or .png");
                     } else {
                     
                          $newfile = IMAGES_PATH.$filename; // setup destination filename
                         if (!file_exists($newfile)) {
                                                  @$result = move_uploaded_file($tmp_filename,$newfile); // @ used to suppress error messages
                                                 // move_uploaded_file also has a security feature built in to only move files that are HTTP posted uploads.
                                                       
                              if (!$result) {
                                      die ("Unable to move file.");
                              } else {
                                   echo "Congradulations: Your file '$newfile' was uploaded!";              
                             } // end if - !$result 
                         
                         } else {
                              die( "Sorry, but that file '$newfile' already exists.");                              
                         } // end if - file_exists
                 } // end if - $type
                     
                     
                     
                } else {

                     die("No Filename Provided.");
                } // end if - $tmp_filename 



Concluding Thoughts For Our PHP Image Upload Script

In order to understand how this works, you should understand that when an HTML form is posted of the type we used in this example, an input field of type="FILE" will tell the HTML server that a file is to be posted to the server's memory and transfered to the harddrive in its TMP directory. PHP allows for you to access details of the transfer by using the $_FILE server array variable.

The $_FILE array variable has the following key fields:
name
type
tmp_name
error
size

For details of your file transfer, you could always list the elements of the $_FILE array by using the print_r($_FILE) command.

Once the attempt to transfer the file to the harddrive of the server, our script checks to see if the $_FILE['name'] is set in the $filename, and then does the processing to check if the filename is of a recognized type. We could have checked this using the $_FILE array, but checking the filename, itself, ensures that your users are uploading files with proper suffixes.

I hope that this helps explain some of the mystery behind the process of uploading a file using PHP. This tutorial only covers the very basic process in order to explain it to a beginner. A more advanced example would illustrate the usage of a PHP Image Upload class, and offer the capability of storing filenames in a database or listing the directory in a graphical format. The possibilities are endless.
 
 

Recent GIDBlogToyota - 2008 July Promotion by Nihal

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Rate This Thread
Rate This Thread:

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

vB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Two virtual hosts, cgi script behaves differently on each blimbo Apache Web Server Forum 0 04-Aug-2004 09:35
JavaScript Tutorial Part 1 pcxgamer Web Design Forum 2 01-Dec-2003 09:16
upload thumbnail zuzupus MySQL / PHP Forum 5 27-Aug-2003 07:12
sms web sender script jrobbio MySQL / PHP Forum 1 11-Apr-2003 12:19
VALIDATING file types on an PHP upload script? JdS MySQL / PHP Forum 0 02-Jan-2003 07:58

Network Sites: GIDNetwork · GIDWebHosts · GIDSearch · Learning Journal by J de Silva, The

All times are GMT -6. The time now is 07:10.


vBulletin, Copyright © 2000 - 2008, Jelsoft Enterprises Ltd.