Jump to content

Upload File


garyblackpool

Recommended Posts

Hi,I am just not getting this, I am trying to uploaad a file but just dont seem to be able to. I have made a folder for my two files with permissions set to 777 and another file called uploads and that permisions set to 777. Is it something to do with that [tmp_name]?Thanks

<html><head><title>File Upload Form</title></head><body>This form allows you to upload a file to the server.<br><form action="getfile.php" method="post"><br>Type (or select) Filename: <input type="file" name="uploadFile"><input type="submit" value="Upload File"></form></body>

<html><head><title>Process Uploaded File</title></head><body><?phpmove_uploaded_file ($_FILES['uploadFile'] ['tmp_name'],       "/uploads/{$_FILES['uploadFile'] ['name']}")?></body></html> 

Link to comment
Share on other sites

What happens when you try?
I just get a blank screen without any errors. I have just got it working after finding a tutorial that uses.(copy($HTTP_POST_FILESSo erm could it be my server configuration?ThanksThe code which is working for me is:
<?php//set where you want to store files//in this example we keep file in folder upload//$HTTP_POST_FILES['ufile']['name']; = upload file name//for example upload file name cartoon.gif . $path will be upload/cartoon.gif$path= "upload/".$HTTP_POST_FILES['ufile']['name'];if($ufile !=none){if(copy($HTTP_POST_FILES['ufile']['tmp_name'], $path)){echo "Successful<BR/>";//$HTTP_POST_FILES['ufile']['name'] = file name//$HTTP_POST_FILES['ufile']['size'] = file size//$HTTP_POST_FILES['ufile']['type'] = type of fileecho "File Name :".$HTTP_POST_FILES['ufile']['name']."<BR/>";echo "File Size :".$HTTP_POST_FILES['ufile']['size']."<BR/>";echo "File Type :".$HTTP_POST_FILES['ufile']['type']."<BR/>";echo "<img src=\"$path\" width=\"150\" height=\"150\">";}else{echo "Error";}}?>

Link to comment
Share on other sites

I'm guessing a path error. Here's the path of the one that works:"upload/".$HTTP_POST_FILES['ufile']['name'];And the one that didn't:"/uploads/{$_FILES['uploadFile'] ['name']}")I'll assume that the different indexes are not an error. There is still a difference. Notice the leading slash on the original one? If "uploads" is in the same directory as your script, and your script is not in your home directory, then these pathnames are describing different directories. I'm guessing "/uploads" (a child of your home directory) doesn't really exist, and that would cause the statement to fail.If that's the trouble, go back to using the $_FILES array.FWIW, I like to assign file paths to a variable so I can echo them when things go wrong. It wouldn't make much difference here, I don't think, but it's a useful habit.It's also a good habit to test the return value of functions. File system commands are more prone than anything else to go wrong. After that come database commands. They both depend on factors external to PHP, so they can return errors even when you've done everything correctly.

Link to comment
Share on other sites

You're also not checking for errors, you need to that too. You're just assuming that the user selected a file to upload and that it was a valid file and correctly got uploaded. Check the manual for information about that:http://www.php.net/manual/en/features.file-upload.phpAlso, if you're getting a blank screen, then error messages aren't being shown, because there is an error happening. You can use this to enable printing of error messages:

ini_set('display_errors', 1);error_reporting(E_ALL);

Link to comment
Share on other sites

I prefer to write files to an error log instead of printing them in the browser. I use something like this:ini_set('log_errors', 1);ini_set('error_log', dirname(__FILE__) . DIRECTORY_SEPARATOR . 'error.log');ini_set('html_errors', 0);ini_set('display_errors', 0);error_reporting(E_ALL);

Link to comment
Share on other sites

This might also help you. I noticed you don't have a certain code in your form tag.enctype="multipart/form-data"As in:

<form action="" enctype="multipart/form-data" method="post"><!-- whatever here --></form>

Link to comment
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...