Reputation: 789
I have this code but this code is not allowing me to upload any types of video files. This always shows 'Please Select File To Upload'.
Code for uploading file shown below:
$fileElementName = 'ufile';
if (!empty($_FILES[$fileElementName]['error'])) {
switch ($_FILES[$fileElementName]['error']) {
case '1':
$error = 'You Are Not Allowed To Upload File Of This Size';
break;
case '2':
$error = 'You Can Not Not Upload File Of This Size';
break;
case '3':
$error = 'The uploaded file was only partially uploaded';
break;
case '4':
$error = 'Not Able To Upload';
break;
case '6':
$error = 'Server Error Please Try Again Later';
break;
case '7':
$error = 'Failed to write file to disk';
break;
case '8':
$error = 'File upload stopped by extension';
break;
case '999':
default:
$error = 'Error Found But Did Not Found What Was Problem';
}
} elseif (empty($_FILES['ufile']['tmp_name']) || $_FILES['ufile']['tmp_name'] == 'none') {
$error = 'Please Select File To Upload';
$success = FALSE;
} else {
$file = $_FILES['ufile']['name'];
move_uploaded_file($_FILES["ufile"]["tmp_name"], $path);
}
echo json_encode($arr);
In the above code $path is upload/ which is correct--it works for all file except video.
This code always shows 'Please Select File To Upload'
Upvotes: 0
Views: 993
Reputation: 360882
Your error handling is broken. On uploads, a successful upload has error code 0 (UPLOAD_ERR_OK
constant). You're doing
if (!empty(...))
empty() evaluates to boolean TRUE for zero values, so you're basically saying "if there was no error, act like there was one".
Your code really should be:
if ($_FILES['ufile']['error'] !== UPLOAD_ERR_OK) {
... error handling switch() here ...
die();
}
Your subsequent empty() checks on the tmp_name
are also pointless. If no file was uploaded, then you'll get error code 4 (UPLOAD_ERR_NO_FILE
) anyways.
Then for the move_uploaded_file() stuff, you pull out the uploaded filename into $file
, but then move the file to $path
, which is not set anywhere in your code. As a word of warning, never EVER use the user-supplied filename directly in file-system operations. It is trivial to embed path information in that field, and if you do not take appropriate sanitization/security precautions, you'll be allowing malicious users to scribble on any file on your server.
Upvotes: 1
Reputation: 365
check permissions, if $path exists, etc, and change move_uploaded_file to copy, some servers have a restrict permission on folders
EDIT: Maybe same issue https://stackoverflow.com/a/3587158/1519436
Upvotes: 0
Reputation: 688
Did you defined the enctype of your form?
In the form tag insert the enctype as follow: enctype="multipart/form-data"
Upvotes: 0