Why doesnt this file upload script work? (SPOT THE ERROR)

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
yepsnap
Forum Newbie
Posts: 2
Joined: Sat Nov 11, 2006 9:22 am

Why doesnt this file upload script work? (SPOT THE ERROR)

Post by yepsnap »

feyd | Please use

Code: Select all

,

Code: Select all

and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]


Why doesnt this file uplaod script work? PHP (SPOT THE ERROR)?
This is the script that is on the second page (upp2.php) ERROR EXPLAINED BELOW SCRIPT:

Code: Select all

<?php
$file_name = $HTTP_POST_FILES['ufile']['name'];
$random_digit=rand(0000,9999);
$new_file_name=$random_digit.$file_name;
//for example upload file name cartoon.gif . $path will be upload/cartoon.gif
$path= "/home/content/t/n/i/tnine8MsoP29il/html/Uploads".$new_file_name;
if($ufile !=none)
{
if(copy($HTTP_POST_FILES['ufile']['tmp_name'], $path))
{
echo "Successful<BR/>"; 
echo "File Name :".$new_file_name."<BR/>"; 
echo "File Size :".$HTTP_POST_FILES['ufile']['size']."<BR/>"; 
echo "File Type :".$HTTP_POST_FILES['ufile']['type']."<BR/>"; 
}
else
{
echo "Error";
}
}
?>
THE PROBLEM: Yes the renamed file uploads but not to the specified folder (Uploads). Instead the file uploads to the main folder with the name (Uploads9898smile.jpg) instead of what i want (9898smile.jpg). I don't know why it adds the word Uploads to each uploaded file and why it doesnt go in the Upload folder.
(BTW: The actual uplaod form definatly isnt the problem i have checked)

Please help


feyd | Please use

Code: Select all

,

Code: Select all

and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

There's no slash after "Uploads."
  • Consider shifting to $_FILES instead of $HTTP_POST_FILES also.
  • basename() should be used to retrieve the filename as some browsers will send a full path.
  • move_uploaded_file() should be used over copy().
  • I hope you aren't relying on the "type" element to be accurate as its value is determined from the browser's input solely.
Post Reply