Note that there are some explanatory texts on larger screens.

plurals
  1. POPHP can't upload files to server?
    primarykey
    data
    text
    <p>I have a php file that uploads images like jpegs and png onto a folder called uploads that is stored on the apache server and in the same location as the php file. </p> <p>I have checked the code of both the HTML and the PHP and both seem to be perfectly fine, however whenever I try to upload a file I always get an error message and the file doesn't get uploaded. </p> <p>It would be much appreciated if someone with more experience than me can look at my code and tell me why it is behaving in this manner. </p> <p>Here is the HTML form:</p> <pre><code>&lt;!-- To change this template, choose Tools | Templates and open the template in the editor. --&gt; &lt;!DOCTYPE html&gt; &lt;html&gt; &lt;head&gt; &lt;meta http-equiv="Content-Type" content="text/html; charset=UTF-8"&gt; &lt;title&gt;Upload Your File&lt;/title&gt; &lt;/head&gt; &lt;body&gt; &lt;?php // put your code here ?&gt; &lt;form enctype="multipart/form-data" method="post" action="fileHandler.php"&gt; Select File: &lt;input name="uploaded_file" type="file"/&gt;&lt;br/&gt; &lt;input type="submit" value="Upload"/&gt; &lt;/form&gt; &lt;/body&gt; &lt;/html&gt; </code></pre> <p>and here is the PHP file that is executed when the form is submitted:</p> <pre><code>&lt;?php /* * To change this template, choose Tools | Templates * and open the template in the editor. * PHP file that uploads files and handles any errors that may occur * when the file is being uploaded. Then places that file into the * "uploads" directory. File cannot work is no "uploads" directory is created in the * same directory as the function. */ $fileName = $_FILES["uploaded_file"]["name"];//the files name takes from the HTML form $fileTmpLoc = $_FILES["uploaded_file"]["tmp_name"];//file in the PHP tmp folder $fileType = $_FILES["uploaded_file"]["type"];//the type of file $fileSize = $_FILES["uploaded_file"]["size"];//file size in bytes $fileErrorMsg = $FILES["uploaded_file"]["error"];//0 for false and 1 for true $target_path = "uploads/" . basename( $_FILES["uploaded_file"]["name"]); echo "file name: $fileName &lt;/br&gt; temp file location: $fileTmpLoc&lt;br/&gt; file type: $fileType&lt;br/&gt; file size: $fileSize&lt;br/&gt; file upload target: $target_path&lt;br/&gt; file error msg: $fileErrorMsg&lt;br/&gt;"; //START PHP Image Upload Error Handling--------------------------------------------------------------------------------------------------- if(!$fileTmpLoc)//no file was chosen ie file = null { echo "ERROR: Please select a file before clicking submit button."; exit(); } else if(!$fileSize &gt; 16777215)//if file is &gt; 16MB (Max size of MEDIUMBLOB) { echo "ERROR: Your file was larger than 16 Megabytes"; unlink($fileTmpLoc);//remove the uploaded file from the PHP folder exit(); } else if(!preg_match("/\.(gif|jpg|jpeg|png)$/i", $fileName))//this codition allows only the type of files listed to be uploaded { echo "ERROR: Your image was not .gif, .jpg, .jpeg or .png"; unlink($fileTmpLoc);//remove the uploaded file from the PHP temp folder exit(); } else if($fileErrorMsg == 1)//if file uploaded error key = 1 ie is true { echo "ERROR: An error occured while processing the file. Please try again."; exit(); } //END PHP Image Upload Error Handling--------------------------------------------------------------------------------------------------------------------- //Place it into your "uploads" folder using the move_uploaded_file() function $moveResult = move_uploaded_file($fileTmpLoc, $target_path); //Check to make sure the result is true before continuing if($moveResult != true) { echo "ERROR: File not uploaded. Please Try again."; unlink($fileTmpLoc);//remove the uploaded file from the PHP temp folder } else { //Display to the page so you see what is happening echo "The file named &lt;strong&gt;$fileName&lt;/strong&gt; uploaded successfully.&lt;br/&gt;&lt;br/&gt;"; echo "It is &lt;strong&gt;$fileSize&lt;/strong&gt; bytes.&lt;br/&gt;&lt;br/&gt;"; echo "It is a &lt;strong&gt;$fileType&lt;/strong&gt; type of file.&lt;br/&gt;&lt;br/&gt;"; echo "The Error Message output for this upload is: $fileErrorMsg"; } ?&gt; </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload