Jump to content

File Upload help


sanprince
 Share

Recommended Posts

Hello im a newbie... so i ws trying out few simple examples. I created an html file with the foll. code

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><html xmlns="http://www.w3.org/1999/xhtml"><head><meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" /><title>Untitled Document</title><style type="text/css"><!--.style1 {	font-family: "Times New Roman", Times, serif;	font-style: italic;}--></style><form action="upload.php" method="post" enctype="multipart/form-data" id="form1">  <h4 align="justify" class="style1">Hello .. This is just a test site to upload files to my server. Hopefully it will WORK </h4>  <table width="435" border="0">    <tr>      <td width="207"><strong>Select file to Upload</strong> </td>      <td width="218"><label>        <input type="file" name="file" />      </label></td>    </tr>    <tr>      <td><label>        <input type="submit" name="Submit" value="Submit" />      </label></td>      <td> </td>    </tr>    <tr>      <td> </td>      <td> </td>    </tr>  </table></form></body></html>

and the corresponding php file with the foll. code

<?php$target = "upload/".$_FILES['file']['name'] ;if(move_uploaded_file($_FILES['file']['tmp_name'], $target)){echo "The file ". $_FILES['file']['name']. " has been uploaded";}else {echo "Sorry, there was a problem uploading your file.";}?>

I loaded the code in apache webserver, and in firefox. te code runs well i.e the script uploads the file in the "upload folder" (which is created also created). The problem im facing is it uploads all the files with extension .html, but not other files like .jpg, .rar etc... can anyone please advice... Do i have to do any changes in the apache's .ini files??Thanks in advance,

Link to comment
Share on other sites

Right after the form tag, you need a hidden input with the max filesize, in bytes.<input type="hidden" name="MAX_FILE_SIZE" value="100000" />The W3Schools tutorial should have that, it's not correct without it. Check the PHP manual for more:http://www.php.net/manual/en/features.file-upload.phpMake sure that the files you're trying to upload aren't too big. You should also add error-checking code into the PHP file to check for errors, that way if the upload fails you can get an error message telling you why.You can use something like this to print the error message:

switch ($_FILES['file']['error']) {   case UPLOAD_ERR_OK:	   break;   case UPLOAD_ERR_INI_SIZE:	   throw new Exception("The uploaded file exceeds the upload_max_filesize directive (".ini_get("upload_max_filesize").") in php.ini.");	   break;   case UPLOAD_ERR_FORM_SIZE:	   throw new Exception("The uploaded file exceeds the MAX_FILE_SIZE directive that was specified in the HTML form.");	   break;   case UPLOAD_ERR_PARTIAL:	   throw new Exception("The uploaded file was only partially uploaded.");	   break;   case UPLOAD_ERR_NO_FILE:	   throw new Exception("No file was uploaded.");	   break;   case UPLOAD_ERR_NO_TMP_DIR:	   throw new Exception("Missing a temporary folder.");	   break;   case UPLOAD_ERR_CANT_WRITE:	   throw new Exception("Failed to write file to disk");	   break;   default:	   throw new Exception("Unknown File Error");}

You don't necessarily need to have it throw an exception though, just printing an error message is normally fine.

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
 Share

×
×
  • Create New...