PHP Image don't upload - php

I am dont' uploading image file to my path. I added enctype='multipart/form-data' in html code. That's php code;
$membeUrl = "inc/img/user/".$_SESSION['memberName'];
if(!is_dir($membeUrl)){mkdir($membeUrl);}
$profilePhoto = $membeUrl."/".$_FILES['profilePhoto']['name'];
if($_FILES['profilePhoto']['error'] > 0){
alert("Profil photo error:".$_FILES['profilePhoto']['error']);
}
move_uploaded_file($_FILES['profilePhoto']['tmp_name'],$profilePhoto);
$w = " WHERE email='a#a.com' AND age='18'";
if((!preg_match("/[\-]{2,}|[;]|[']|[\\\*]/",$profilePhoto))){
mysql_query("UPDATE member SET pp='".$profilePhoto."'".$w);
}
I don't see and understand. How we do solve?
Thank you for your insterest.
Good works..

From var_dump() info you specified it is obvious file you are uploading exceeds php.ini upload_max_filesize directive (check the Error=1 code explanation in manual)
Either upload a smaller file or change php.ini value.
Also check that post_max_size ini directive is equal or larger than upload_max_filesize, since even if you put larger size for upload, post value is the absolute limit.
To know more about ini directives read manual: http://www.php.net/manual/en/ini.core.php#ini.upload-max-filesize

Related

Is it possible to force PHP code to continue running, if a php.ini directive such as upload_max_filesize is exceeded when uploaded file exceeds this?

I have a single PHP page, with the php (v7.4.7) script at the top of the page. The page functions as a file up-loader, allows up to 10 files per upload, and the max file size is set in the script.
The site works perfectly, generating a table of the result of each file to the user.
That is, unless someone uploads a file greater than the upload_max_filesize directive in the php.ini file. At which point, the script stops dead and cannot therefore, continue to provide the necessary results back to the user. No results are returned, the results table is therefore empty, and the user might think, wrongly, all went well.
I have tried adding try/catch blocks, but the script still fails to complete. Is this by design, or is there a way to coerce the script to run/complete, if this directive is exceeded? The HTML/PHP code is all pretty standard, as demonstrated by various tutorials etc.
Many thanks.
Violating upload_max_filesize doesn't abort the script execution. It'll just cause the error key in $_FILES to become UPLOAD_ERR_INI_SIZE.
Most likely, you're hitting an entirely different limit. For example, Apache has
LimitRequestBody and PHP itself has post_max_size or even max_file_uploads. In general, those directives don't abort the script either but simply wipe out the data excess.
My advice is to locate all the directives that may affect file uploads, set them one by one to an artificially low limit and verify what effect they have in your data. A check I typically do is to verify if $_SERVER['CONTENT_LENGTH'] is greater than zero for a request where $_SERVER['REQUEST_METHOD'] is POST but $_POST is empty. Aside that, checking error in $_FILES should have you fairly covered.
; Allow enough time for the upload to complete
max_input_time = 1200
; Max file size
upload_max_filesize = 50M
; Greater than upload_max_filesize to ease diagnose slightly large files
post_max_size = 100M
if (
empty($_FILES) &&
empty($_POST) &&
isset($_SERVER['REQUEST_METHOD']) &&
strtolower($_SERVER['REQUEST_METHOD'])=='post'
) {
// Error: post body was too large
// Bytes sent (and discarded): filter_input(INPUT_SERVER, 'CONTENT_LENGTH')
} elseif (
isset($_FILES['foo']) &&
$_FILES['foo']['error'] != UPLOAD_ERR_NO_FILE
){
// Upload
if ($_FILES['foo']['error'] === UPLOAD_ERR_OK) {
// Successful upload
}else{
// Failed upload
}
} else {
// No upload
}

move_uploaded_file not working when uploading more than one files - Intermittently

What I need
I need to upload three files to the some directory at the same time and update MySQL database
The problem
Sometimes files can upload. But sometimes not all files uploading. Lets say I have file A, B and C. Sometimes all A, B and C can upload. But sometimes randomly (Either A, B or C) some files not uploading. But the MySQL query always update correctly. No issue with that
Codes
Following are my codes. Anyone can spot any mistakes? or any better way of doing this?
if($cadguiUpdated == 'cadYes' && $excelSheetUpdated == 'excelYes' && $tpi == 'tpiYes') {
mkdir($pathCad, 0, true);
mkdir($pathExcel, 0, true);
mkdir($pathTpi, 0, true);
move_uploaded_file($_FILES["fileCad"]["tmp_name"], $target_fileCad);
move_uploaded_file($_FILES["fileExcel"]["tmp_name"], $target_fileExcel);
move_uploaded_file($_FILES["fileTpi"]["tmp_name"], $target_fileTpi);
$sql="INSERT INTO dbtuts.tbl_uploads(prjId, date, cadFile, cadType,cadUpdate, cadTempPerm, cadRevision, cadRemarks, cadRemarksHistory, excelFile, excelType, excelUpdate, excelTempPerm, excelRevision, excelRemarks, excelRemarksHistory, tpiFile, tpiType, tpiUpdate, tpiRevision, tpiRemarks, tpiRemarksHistory, optionalRemarks, updatedPerson, activity) VALUES('$prjId','$updatedDate', '$save_fileCad','$fileTypeCad','$cadguiUpdated','$tempPermCad', '$revisionCad', '$cadguiRemarks','$cadInitialRemarks', '$save_fileExcel','$fileTypeExcel','$excelSheetUpdated','$tempPermExcelSheet', '$revisionExcelSheet', '$excelSheetRemarks','$excelInitialRemarks', '$save_fileTpi','$fileTypeTpi','$tpi', '$formNumber', '$tpiRemarks','$tpiInitialRemarks', '$optionalRemarks', '$updatedPerson', '$activity')";
mysqli_query($conn, $sql);
echo '<br><div class=" container alert alert-success alert-dismissable fade in">×<strong>Success </strong>: Successfully created</div>';
}
After tried many things, finally realised issue was due to file size. Sometimes the files I used to upload was too big compared to the declared file size. To change allowed upload file size limit I did few changes in php.ini file.
Step 1
Open php.ini file and search for this string upload_max_filesize. Change this to your preferred value.
Step 2
Search for post_max_size in the php.ini file and change it according to your desired value. Take note that, this post_max_size is the file size before you actually upload it.
Initially I did only Step 1. But still couldn't upload due to post_max_size was not set correctly. After did Step 2, I am able to upload files without any issue for the specified file size
NB: Please make sure to restart your XAMPP after do the changes.

PHP File upload: file too large, while it is not

I've been pulling hairs on this one. I am creating a quit simple file upload application. On a specific point, the files are not uploaded and I receive the message: "Sorry, your file is too large.". I am uploading a file of ±2MB.
The relevant parameters in the php.ini are:
file_uploads On
max_execution_time 60
max_file_uploads 20
post_max_size 32M
upload_max_filesize 32M
So, there shouldn't be a problem there. Anybody an idea?
Used code:
if (isset($_POST['fileUploadBtn'])) {
echo 'File Size: '.$_FILES['file']['size'];
if(isset($_FILES['file']['error'])) {
echo $error_types[$_FILES['file']['error']].'<br>';
}
if(isset($_FILES["file"])) {
if($_FILES["file"]["name"] != '') {
uploadFile($mysqli, $orderid, $md5, $_FILES["file"], 'weefontwerp');
}
}
}
In your code, did you check the file size?
like this:
if ($_FILES['fileToUpload']['size'] > 500000) {
echo 'file too large.';
}
Or, did you restart php to reload the php.ini?
OK, the problem was in the downloaded upload function. There was the check, I forgot to check. I'm terribly sorry to post this here!

How to avoid post_max_size

I have a problem. I am creating my own PHP application and I want to allow users to upload files.
My PHP
function make_upload() {
echo "<pre>";
print_r($_FILES);
echo "</pre>";
}
if (isset($_GET["upload"]) && $_GET["upload"] == "1") {
make_upload();
}
My HTML
<form action="?upload=1" method="post" enctype="multipart/form-data">
<input name="file[]" type="file" multiple/>
<input type="submit" value="Upload"/>
</form>
When I try to browse and select many files, the print_r($_FILES); shows blank Array ( ) instead of the array with my files. That's happens because of post_max_size which is set to 8M. If I change my post_max_size then I can select more images.
I want to share my application to be installed on websites, but I don't want to tell users to increase their post_max_size.
Is there any option to avoid post_max_size? (I see that in wordpress I can select images which are over the post_max_size - how is made?)
you can do it via .htaccess file.
php_value upload_max_filesize 200M
but sometime hosting provider does not allow you to overrite it. you will get 500 Internal Server Error in that case
You need to set the value of upload_max_filesize and post_max_size in your php.ini :
Maximum allowed size for uploaded files.
upload_max_filesize = 40M;
Must be greater than or equal to upload_max_filesize
post_max_size = 40M;
After modifying php.ini file(s), you need to restart your HTTP server to use new configuration.
If you can't change your php.ini, you're out of luck. You cannot change these values at run-time; uploads of file larger than the value specified in php.ini will have failed by the time execution reaches your call to ini_set.
See the Description of core php.ini directives.
you have not passed array to function pass it when you calling
if (isset($_GET["upload"]) && $_GET["upload"] == "1") {
make_upload($_FILES); // pass this $_FILES array to function
}
AND function would be like this
function make_upload($_FILES) {
echo "<pre>";
print_r($_FILES); // will print array
echo "</pre>";
}

$_FILES field 'tmp_name' has no value on .JPG file extension

i was making an upload script when i tested an image file wit this extension .JPG, i don't know whats the difference between jpg or jpeg, but it seems that $_FILES don't recognize this file type.
I've read several threads that $_FILES ins't that reliable when it comes to mime type, so i decided to used the php's mime type function mime_content_type(), php's getimagesize(), pathinfo(), though pathinfo returns a file name, and type, but i need the path of the file which is NOT present, all of the functions are being passed with $_FILES['file']['tmp_name'] as parameters.
So this problem came up when i decided to upload an image file e.g sample.JPG, i think most of this files are raw from the camera <-- that's what i think though but nevertheless what is more important is that i can upload them .JPG, .jpg, jpeg, .png. all of them works fine except for .JPG.
Main problem is that field ['tmp_name'] in $_FILES has no values when .JPG is to be uploaded.
Any of you guys who have encountered this problem please do share your workaround or "how did you do it" kind of thing.
If $_FILES[$field]['tmp_name'] is empty then the file hasn't been uploaded. You should look at $_FILES[$field]['error'] to see why.
FWIW, and as far as I understand it, the mime-type in $_FILES[] is provided by the browser.
Update: here is a bit of potted code to handle all file upload errors:
$message = 'Error uploading file';
switch( $_FILES['newfile']['error'] ) {
case UPLOAD_ERR_OK:
$message = false;;
break;
case UPLOAD_ERR_INI_SIZE:
case UPLOAD_ERR_FORM_SIZE:
$message .= ' - file too large (limit of '.get_max_upload().' bytes).';
break;
case UPLOAD_ERR_PARTIAL:
$message .= ' - file upload was not completed.';
break;
case UPLOAD_ERR_NO_FILE:
$message .= ' - zero-length file uploaded.';
break;
default:
$message .= ' - internal error #'.$_FILES['newfile']['error'];
break;
}
if( !$message ) {
if( !is_uploaded_file($_FILES['newfile']['tmp_name']) ) {
$message = 'Error uploading file - unknown error.';
} else {
// Let's see if we can move the file...
$dest .= '/'.$this_file;
if( !move_uploaded_file($_FILES['newfile']['tmp_name'], $dest) ) { // No error supporession so we can see the underlying error.
$message = 'Error uploading file - could not save upload (this will probably be a permissions problem in '.$dest.')';
} else {
$message = 'File uploaded okay.';
}
}
}
Check your php.ini and in particular this setting
; Maximum allowed size for uploaded files.
; http://www.php.net/manual/en/ini.core.php#ini.upload-max-filesize
upload_max_filesize = 6M
Or do this in your Apache Config:
<Directory "/var/www/vhosts/path/to/your/directory/import/">
php_value post_max_size 6M
php_value upload_max_filesize 6M
</Directory>
I would also say that it is poor that PHP doesn't report an error in the error logs if you upload a file that is larger than your php.ini upload_max_filesize setting. For example, if you upload a 6MB file when you have it set at 2M (which I think is the default).
Posting an answer because my rating is too low.
Remember to restart your server after setting the max file size cap in your php.ini. I spent hours on this issue thinking that it wasn't a file size problem meanwhile I forgot to restart. After restart everything worked.
I hope this can help someone.
I was having the same problem and being familiar with mostly .NET platform makes you forget about stuff that happens in the client side html.
My problem was my form is having a MAX_FILE_SIZE hidden input which has some value lesser than file's equavalent bytes.
Your form should have this;
Other than that, your form tag must include enctype="multipart/form-data"
I was thining that max file size was in kb, but it's in bytes, thanks to some other page in stackoverflow.
The other reason that might cause this problem your php.ini settings that people have mentioned in previous comments. You should can post_max_size = 200M to php.ini file too.
If you are developing on Windows like me, you can see a dump file that showing errors at C:\Windows\Temp called as "php**VERSION**_errors.log". It helps.
Seems as though it's a random problem because while I was making a script to upload CSV files, some CSV files would have no problem uploading but in other cases, $_FILES['file'][tmp_name] would be empty.
I faced the issue with $_FILES field 'tmp_name' having no value for .JPG file extension and successfully fixed it in few steps. These measures may help someone in the future.
First of all, I implemented the Switch Case solution offered by the
user 'staticsan'. Link here -
https://stackoverflow.com/a/14472801/3681985. This solution helped me
trace the potential issues such as parameters in php.ini file and
folder permissions.
The php.ini file was named php.ini.default. Changing the parameters upload_max_filesize and post_max_size didn't yield any result, until I renamed the file to php.ini. Remember to experiment with parameter values.
After fixing the file name issue, I encountered a challenge with the permissions to the folder in which the uploaded temp image has to be moved. I have changed the permissions and was able to see the image uploaded in to the folder.
Just try this and see what happens,
if (($_FILES['file']['type']) == "image/jpg" || ($_FILES['file']['type']) == "image/jpeg") {
//do uploading stuff here
}else{
echo 'File is not a valid JPG,JPEG';
}

Categories