Current location: Hot Scripts Forums » Programming Languages » PHP » php upload file script


php upload file script

Reply
  #1 (permalink)  
Old 08-27-06, 10:12 PM
xavier039 xavier039 is offline
Newbie Coder
 
Join Date: Aug 2006
Posts: 56
Thanks: 0
Thanked 0 Times in 0 Posts
php upload file script

So I have this script which I am using on my site for file uploads. But I can not get it working. I created a html form which called upon this script but it won't work. I had it working at one point but accidentally deleted it and now I can't get it back. I am getting very frustrated so I turned here for help. PLEASE help me. Here is the php script for the upload. I save it as upload.php (fyi)

PHP Code:

<?php


//define a maxim size for the uploaded images in Kb
define ("MAX_SIZE","100");

//This function reads the extension of the file. It is used to determine if the file is an image by checking the extension.
function getExtension($str) {
$i strrpos($str,".");
if (!
$i) { return ""; }
$l strlen($str) - $i;
$ext substr($str,$i+1,$l);
return 
$ext;
}

//This variable is used as a flag. The value is initialized with 0 (meaning no error found) and it will be changed to 1 if an errro occures. If the error occures the file will not be uploaded.
$errors=0;
//checks if the form has been submitted
if(isset($_POST['Submit']))
{
//reads the name of the file the user submitted for uploading
$image=$_FILES['image']['name'];
//if it is not empty
if ($image)
{
//get the original name of the file from the clients machine
$filename stripslashes($_FILES['image']['name']);
//get the extension of the file in a lower case format
$extension getExtension($filename);
$extension strtolower($extension);
//if it is not a known extension, we will suppose it is an error and will not upload the file, otherwize we will do more tests
if (($extension != "jpg") && ($extension != "jpeg") && ($extension != "png") && ($extension != "gif"))
{
//print error message
echo '<h1>Unknown extension!</h1>';
$errors=1;
}
else
{
//get the size of the image in bytes
//$_FILES['image']['tmp_name'] is the temporary filename of the file in which the uploaded file was stored on the server
$size=filesize($_FILES['image']['tmp_name']);

//compare the size with the maxim size we defined and print error if bigger
if ($size MAX_SIZE*1024)
{
echo 
'<h1>You have exceeded the size limit!</h1>';
$errors=1;
}

//we will give an unique name, for example the time in unix time format
$image_name=time().'.'.$extension;
//the new name will be containing the full path where will be stored (images folder)
$newname="images/".$image_name;
//we verify if the image has been uploaded, and print error instead
$copied copy($_FILES['image']['tmp_name'], $newname);
if (!
$copied)
{
echo 
'<h1>Copy unsuccessfull!</h1>';
$errors=1;
}}}}

//If no errors registred, print the success message
if(isset($_POST['Submit']) && !$errors)
{
echo 
"<h1>File Uploaded Successfully! Try again!</h1>";
}

?>
Here the html form script that I have been trying. I have messed around with it a bunch but nothing seems to work.
Code:
<form enctype="multipart/form-data" action="upload.php" method="POST">
<input type="hidden" name="MAX_FILE_SIZE" value="100000" />
Choose a file to upload: <input name="uploadedfile" type="file" /><br />
<input type="submit" value="Upload File" />
</form>
When I test the file I browse for a correct file then submit it. It then loads upload.php but neither gives me an error nor tells me I was successful. The file also was not uploaded into the folder without my knowing I checked that too. I had it all working a one point. I think my problem is it doesn't know where to save the file too. It was a folder called images/ on my server. Please any help would be great.

Thank you!

Last edited by nico_swd; 08-28-06 at 02:36 AM. Reason: Please use PHP wrappers when posting php code.
Reply With Quote
  #2 (permalink)  
Old 08-27-06, 11:15 PM
mab's Avatar
mab mab is offline
Community VIP
 
Join Date: Oct 2005
Location: Denver, Co. USA
Posts: 2,674
Thanks: 0
Thanked 0 Times in 0 Posts
Your HTML form code works correctly. The most obvious reason why the remainder of your code does not work is that the name of the input field in the form uploadedfile must match the $_FILES['...'] index name, which is image.

The first step in troubleshooting any incorrect data is to display it to see if it contains the expected contents. Try the following code to display the entire contents of the $_FILES variable -
PHP Code:

echo "<pre>";

print_r($_FILES);
echo 
"</pre>"
__________________
Error checking, error reporting, and error recovery. If your code does not have these to get it to tell you why it is not working, what makes you think someone in a programming forum will be able to tell you why it is not working???
Reply With Quote
  #3 (permalink)  
Old 08-28-06, 07:03 AM
ThaLyric ThaLyric is offline
Newbie Coder
 
Join Date: Jul 2006
Posts: 15
Thanks: 0
Thanked 0 Times in 0 Posts
I've created a class for that :

http://www.leihitu.nl/?page=40

maybe it can help you
Reply With Quote
  #4 (permalink)  
Old 08-28-06, 07:54 PM
xavier039 xavier039 is offline
Newbie Coder
 
Join Date: Aug 2006
Posts: 56
Thanks: 0
Thanked 0 Times in 0 Posts
I followed both of your solutions. When I entered

PHP Code:

echo "<pre>";

print_r($_FILES);
echo 
"</pre>"
my result was thus:

Code:
Array
(
    [image] => Array
        (
            [name] => ss404.jpg
            [type] => image/pjpeg
            [tmp_name] => C:\WINDOWS\TEMP\phpD7.tmp
            [error] => 0
            [size] => 50884
        )

)
I am still unable to get it working. Any other suggestions? This is getting mighty frustrating.
Reply With Quote
  #5 (permalink)  
Old 08-28-06, 08:42 PM
mab's Avatar
mab mab is offline
Community VIP
 
Join Date: Oct 2005
Location: Denver, Co. USA
Posts: 2,674
Thanks: 0
Thanked 0 Times in 0 Posts
If we ignore the fact that the form code you posted has the following line -
Code:
Choose a file to upload: <input name="uploadedfile" type="file" /><br />
and produces a $_FILES['uploadedfile'] array variable and not a $_FILES['image'] array variable, the next most obvious reason why this code does not work is that the submit button in the form does not have a name = "..." assignment and the following line in the PHP code will never be true -
PHP Code:

if(isset($_POST['Submit'])) 

The reason why I am only giving a fix-this-next-hint each time, is that you won't learn much if I go through the code and find/fix everything. Part of programming, is troubleshooting. If you have to wait around a day for someone to post an answer to each of your problems, it is going to take you a very long time to produce working code.

All functions and programs do three things - they accept inputs/data, perform processing on those inputs/data, and output results/data. If you are not getting the results from your program that you expect, I recommend starting at the beginning and echoing/printing the input/data it is operating upon at each decision making point to make sure that it is what you and the program logic expect.
__________________
Error checking, error reporting, and error recovery. If your code does not have these to get it to tell you why it is not working, what makes you think someone in a programming forum will be able to tell you why it is not working???
Reply With Quote
  #6 (permalink)  
Old 08-29-06, 04:35 PM
xavier039 xavier039 is offline
Newbie Coder
 
Join Date: Aug 2006
Posts: 56
Thanks: 0
Thanked 0 Times in 0 Posts
Thanks so much I finally got it working. You really helped me out a bunch!
Reply With Quote
Reply

Bookmarks


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

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 Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
FTP File Upload Script - Help me Please glennlopez Script Requests 4 06-01-05 05:16 AM
PHP script problem (please help) osmanmumtaz PHP 0 05-24-05 07:29 AM
PHP Script Request DazzlyWorks Script Requests 0 01-16-05 01:23 PM
will pay. php script needed. upload, resize, & email jamjammo Script Requests 4 02-29-04 08:30 PM
Image File Upload Script Millz PHP 6 12-30-03 10:37 AM


All times are GMT -5. The time now is 07:27 AM.
vBulletin® Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.