I am trying to write a little script in which the user can upload a file but for some reason,I am having issues. For some reason when the submit button is clicked, the _$FILES
array is coming back NULL (tested this with var_dump()
and var_export()
) so of course everything else is skipped and I get the "Error: No file uploaded" I cannot determine why this is. Below is the snippet of HTML for the form as well as the PHP script.
The HTML
<form class='admin_form' action='' method='POST' enctype='multipart/form-data'>
<fieldset>
<label for='comic_page'>Filename</label>
<input type='file' name='uploaded_file' id='uploaded_file'>
<input type='submit' name='comic_page_btn' value='Upload'>
<span id='comic_page_upl_err_msg'>".$err_msg."</span>
</fieldset>
</form>
The PHP
if(isset($_POST['comic_page_btn'])){
echo var_dump($_FILES['uploaded_file']['name']).'<br>';
echo var_export($_FILES["uploaded_file"]);
if((!empty($_FILES["uploaded_file"])) && ($_FILES['uploaded_file']['error'] == 0)) {
//Check if the file is JPEG image and it's size is less than 350Kb
$filename = basename($_FILES['uploaded_file']['name']);
$ext = substr($filename, strrpos($filename, '.') + 1);
if (($ext == "jpg") && ($_FILES["uploaded_file"]["type"] == "image/jpeg") &&
($_FILES["uploaded_file"]["size"] < 350000)) {
//Determine the path to which we want to save this file
$newname = dirname(__FILE__).'/uploads/'.$filename;
//Check if the file with the same name is already exists on the server
if (!file_exists($newname)) {
//Attempt to move the uploaded file to it's new place
if ((move_uploaded_file($_FILES['uploaded_file']['tmp_name'],$newname))) {
$err_msg = "The file has been saved as: ".$newname;
} else {
$err_msg = "Error: A problem occurred during file upload!";
}
} else {
$err_msg = "Error: File ".$_FILES["uploaded_file"]["name"]." already exists";
}
} else {
$err_msg = "Error: Only .jpg images under 350Kb are accepted for upload";
}
} else {
$err_msg = "Error: No file uploaded";
}
}
';`? Does that work? – Popnoodles Jul 13 '14 at 20:07