Jump to content

Allow Users to upload file, verious attemps have not worked, HELP!!


Sharkadder

Recommended Posts

Hi, i have recently added a passworded area onto me website, this passworded area is for people with administrator rights to upload files onto the site itself.(any file type)i have read up on it in google and various links have led me to simular pages about uploading with PHP, JAVAscript or CGI code.PHP has been the most popular choice but everytime i copy and paste a sample code to get the basics working it never works. I have tried all sorts.I read i need to have an "uploads" folder which i already have, another site explained something about a "bin" folder, i have not studied the language and its frustrating trying to work it out.If anybody has a sample code of a HTML file and an external file what saves the results (not just as a tempory file) then that would be great.Everytime i have tried attaching it to an external file to a submit button link e.g. "upload.php" i just get page cannot be displayed.Im after anyway i can get people to upload, either with php, cgi or java, whichever is easiest to explain basicallyMy website host address is shown below and i am currently on the "economy plan"http://www.domain-website-hosting.com/hope you can help

Link to comment
Share on other sites

Programming is about more then copying and pasting code, you need to understand the what and the why so that you can solve problems that come up. One thing to note is don't develop using IE, most of the time it will show you its generic error page instead of a helpful error message. Other browsers will show the actual error message.This is the reference on php.net for handling file uploads:http://www.php.net/manual/en/features.file-upload.phpThis is the sample HTML form they give:

<!-- The data encoding type, enctype, MUST be specified as below --><form enctype="multipart/form-data" action="__URL__" method="POST">    <!-- MAX_FILE_SIZE must precede the file input field -->    <input type="hidden" name="MAX_FILE_SIZE" value="30000" />    <!-- Name of input element determines name in $_FILES array -->    Send this file: <input name="userfile" type="file" />    <input type="submit" value="Send File" /></form>

The important things to note are that the form has an enctype of "multipart/form-data", which is required for file uploads, and there is also a hidden variable called MAX_FILE_SIZE that you need to set in bytes. Also, like any other form, you need to set the action to whatever page you are using to process the file upload.There are more examples on php.net about how to proceed once the file has been uploaded, and you can also check the references for the move_uploaded_file function to see some more examples. The link for that page is on the page I pasted above.

Link to comment
Share on other sites

Programming is about more then copying and pasting code, you need to understand the what and the why so that you can solve problems that come up. One thing to note is don't develop using IE, most of the time it will show you its generic error page instead of a helpful error message. Other browsers will show the actual error message.This is the reference on php.net for handling file uploads:http://www.php.net/manual/en/features.file-upload.phpThis is the sample HTML form they give:
<!-- The data encoding type, enctype, MUST be specified as below --><form enctype="multipart/form-data" action="__URL__" method="POST">    <!-- MAX_FILE_SIZE must precede the file input field -->    <input type="hidden" name="MAX_FILE_SIZE" value="30000" />    <!-- Name of input element determines name in $_FILES array -->    Send this file: <input name="userfile" type="file" />    <input type="submit" value="Send File" /></form>

The important things to note are that the form has an enctype of "multipart/form-data", which is required for file uploads, and there is also a hidden variable called MAX_FILE_SIZE that you need to set in bytes. Also, like any other form, you need to set the action to whatever page you are using to process the file upload.There are more examples on php.net about how to proceed once the file has been uploaded, and you can also check the references for the move_uploaded_file function to see some more examples. The link for that page is on the page I pasted above.

well i already looked into it kind of so i understood some code, i know about limiting the file size and i have set it up numerious times by using forms in HTML but thats just the nice looks, its the mechanics underneath i could not get working, ill add it to my code and if it dont work ill tell you tomorrow along with my code, thanks for the reply.
Link to comment
Share on other sites

Below is my HTML code for the page i am trying to get the form to work on, i have included the code from the start of the <BODY> tag until the end of the form, this is shown below.<body><div id="hdr"><div style="font-weight:bold;font-size:12px;margin:6px 10px;"><span style="color:#FF0000; font-size:30px">Welcome To Sharkadder Software</a></span><span style="text-align:right;"><a href="contactus.html">contact us</a> |<a href="sitemap.html">site Map</a></span></div></span><img src="classic gaming.gif" alt="" border="0" width="450" height="40"/></div><div id="rh-col"><center><span style="color:#FF0000;font-weight:bold; font-size:30px">Private File Download Area<align="center"></center></span></center><br><center><form action="upload_file.php" method="post"enctype="multipart/form-data"><label for="file">Filename:</label><input type="file" name="file" id="file" /> <br /><input type="submit" name="submit" value="Submit" /></form></center><br>Now i could not see anything wrong with that code, below is a php file i created in notepad and then uploaded to my website, it is just to upload images as an example.The file is called "upload_file.php and this code is shown below.<html><head> <title></title></head><body><?phpif (($_FILES["file"]["type"] == "image/gif")|| ($_FILES["file"]["type"] == "image/jpeg")&& ($_FILES["file"]["size"] < 20000)) { if ($_FILES["file"]["error"] > 0) { echo "Return Code: " . $_FILES["file"]["error"] . "<br />"; } else { echo "Upload: " . $_FILES["file"]["name"] . "<br />"; echo "Type: " . $_FILES["file"]["type"] . "<br />"; echo "Size: " . ($_FILES["file"]["size"] / 1024) . " Kb<br />"; echo "Temp file: " . $_FILES["file"]["tmp_name"] . "<br />"; if (file_exists("upload/" . $_FILES["file"]["name"])) { echo $_FILES["file"]["name"] . " already exists. "; } else { move_uploaded_file($_FILES["file"]["tmp_name"], "upload/" . $_FILES["file"]["name"]); echo "Stored in: " . "upload/" . $_FILES["file"]["name"]; } } }else { echo "Invalid file"; }?></body></html>I run the test on mozilla and i get the following message:Upload: bannerbackground.jpgType: image/jpegSize: 7.8603515625 KbTemp file: d:\temp\php270F.tmpStored in: upload/bannerbackground.jpgWhat i have noticed its storing the info in tempory which isnt what i want, i want it saved onto the actual website not my D drive as it says its stored.everytime i try to run that page on IE it never works, i understand bits of the php code but im unsure how it is going to work, if somebody can amend the code to make it work that would be great or if i have missed a whole page of code out then let me know.Any help is apprechiated, thanks Mark

Link to comment
Share on other sites

It should be in the uploads folder. "Stored in: upload/bannerbackground.jpg" :). It should work fine in IE! So check in the upload folder for that file. :)
Its not there, I created a folder on the website called "login" in there is the page which uploads a file, within that folder(login) i have another folder called "upload", when i run the required action in mozilla firefox it says it works sucessfully and the file is stored in "uploads/nameofimage.gif/jpeg.(as shown below)Upload: mozilla.gifType: image/gifSize: 12.5576171875 KbTemp file: d:\temp\php3167.tmpStored in: upload/mozilla.gifThis however does not work, i have an upload folder, the code is show below, absolutely nothing is wrong with my code and why does it say it has uploaded sucessfully if it hasnt?<html><head><title></title></head><body><?phpif (($_FILES["file"]["type"] == "image/gif")|| ($_FILES["file"]["type"] == "image/jpeg")&& ($_FILES["file"]["size"] < 100000)) { if ($_FILES["file"]["error"] > 0) { echo "Return Code: " . $_FILES["file"]["error"] . "<br />"; } else { echo "Upload: " . $_FILES["file"]["name"] . "<br />"; echo "Type: " . $_FILES["file"]["type"] . "<br />"; echo "Size: " . ($_FILES["file"]["size"] / 1024) . " Kb<br />"; echo "Temp file: " . $_FILES["file"]["tmp_name"] . "<br />"; if (file_exists("upload/" . $_FILES["file"]["name"])) { echo $_FILES["file"]["name"] . " already exists. "; } else { move_uploaded_file($_FILES["file"]["tmp_name"], "upload/" . $_FILES["file"]["name"]); echo "Stored in: " . "upload/" . $_FILES["file"]["name"]; } } }else { echo "Invalid file"; }?></body></html>That is the code, and yet still on internet explorer i just get page cannot bedisplayed :S, very strange
Link to comment
Share on other sites

This however does not work, i have an upload folder, the code is show below, absolutely nothing is wrong with my code and why does it say it has uploaded sucessfully if it hasnt?
Because you tell it to say it uploaded successfully without checking if it actually did. And if there's absolutely nothing wrong with the code, then why doesn't it work?First of all, IE sends the mime type for a jpeg as "image/pjpeg", which is different then other browsers. You need to add that to your list of types to check for. Secondly, you left out the MAX_FILE_SIZE hidden element. See the example I posted above. Thirdly, I'm not entirely positive about this, but I'm pretty sure that the move_uploaded_file needs an absolute path, not a relative path. You can use this check to determine whether or not the file was moved:
if (move_uploaded_file($_FILES["file"]["tmp_name"], "upload/" . $_FILES["file"]["name"]) === false){  echo "file not moved";}else{  echo "Stored in: " . "upload/" . $_FILES["file"]["name"];}

As for the temp file, if you don't want it stored on the D drive then change the location of PHP's temp folder. Files don't just get uploaded and stored in memory until you do something with them, they get saved to a temporary file. So it's exactly what you want, it means the upload is working correctly, you're just not successfully moving the temporary file to wherever you want it. Specify the absolute path to the destination you want the files saved in, and if you are still having problems make sure that PHP has permission to write to that folder.

Link to comment
Share on other sites

ok i still kinda like dont get it.i amended the HTML form code <br><form enctype="multipart/form-data" action="upload_class.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>The php code document now looks as follows (alot shorter than my previous 1)<html><head><title></title></head><body><?php$target_path = "uploads/";/* Add the original filename to our target path.Result is "uploads/filename.extension" */$target_path = $target_path . basename( $_FILES['uploadedfile']['name']);$_FILES['uploadedfile']['tmp_name'];$target_path = "uploads/";$target_path = $target_path . basename( $_FILES['uploadedfile']['name']);if(move_uploaded_file($_FILES['uploadedfile']['tmp_name'], $uploads/)) { echo "The file ". basename( $_FILES['uploadedfile']['name']). " has been uploaded";} else{ echo "There was an error uploading the file, please try again!";}?></body></html>Yes i know the code looks firmilular, its standard code but i am having trouble with the tempory thing still its a pain in the butt, i have set the folder to my "uploads" folder and i dont know what i need to do with tempory or whatever, i have read up on it and its lost me sorry.Basically i want the temp folder to be the same folder as "uploads" i.e where the end file is going to be placed.I run it in mozilla firefox 2.0 and it comes up blank, nothing appears on the screen, no file has not been uploaded or no file has been uploaded, nothing just blank once i press submit.where have i gone wrong justsomeguy? or whoever answers to this post

Link to comment
Share on other sites

The page is blank because there are errors but the error messages are probably disabled. This line has a syntax error:if(move_uploaded_file($_FILES['uploadedfile']['tmp_name'], $uploads/)) {There is a slash after $uploads that should not be there. Also, $uploads should probably be $target_path instead.This block:

$target_path = "uploads/";/* Add the original filename to our target path.Result is "uploads/filename.extension" */$target_path = $target_path . basename( $_FILES['uploadedfile']['name']);$_FILES['uploadedfile']['tmp_name'];$target_path = "uploads/";$target_path = $target_path . basename( $_FILES['uploadedfile']['name']);

is redundant and contains some no-ops. It can be shortened to this:

$target_path = "uploads/";$target_path = $target_path . basename( $_FILES['uploadedfile']['name']);

But you are still using a relative path. You can't copy the file to a directory called "uploads", you need to copy it to "C:\path\to\uploads\". It needs to be an absolute path, not a relative path.You also don't want to set your temporary directory to be your uploads directory, since the temporary directory gets deleted periodically. That's what temporary means. PHP needs a temporary directory to work with, but if you don't want it to be whatever it is now, you can change it to be wherever else you want it to be. Just don't point it to something that contains files you don't want deleted.

Link to comment
Share on other sites

Relative paths are perfectly fine.Are you sure that the uploads directory allows uploads? Try chmodding it to 777. If that works then great.I made an uploads script some time ago when I was just getting into PHP. Granted, I dissallowed certain extensions rather than simply only allowing certain mime types, but that can easily be amended:

$target_path = "uploads/";$target_path = $target_path . basename($_FILES['uploadedfile']['name']); $allowed_types = array('image/gif', 'image/jpeg', 'image/pjpeg');if($_POST['MAX_FILE_SIZE'] >= filesize($_FILES['uploadedfile']['tmp_name']) && in_array($_FILES['uploadedfile']['type'], $allowed_types)){	if(move_uploaded_file($_FILES['uploadedfile']['tmp_name'], $target_path))	{		//Success message	}	else	{		//Error message	}}else{	//Not allowed message}

And for the form, you just need something like this:

<form enctype="multipart/form-data" action="upload_class.php" method="post">	<input type="hidden" name="MAX_FILE_SIZE" value="5242880" />	Choose a file to upload: <input type="file" name="uploadedfile" /><br />	<input type="submit" value="Upload File" /></form>

Simple!Just make sure that permissions for the uploads folder allow uploads! :)

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...