Verot class.upload.php uploading big files - php

I use an upload form to upload images. Small image like < 4mb will work.
But if I upload an image with a file size like 5.7MB, it doesn't do anything. It just doesn't upload the file. I have searched a lot but I couldn't figure it out. I think the problem has to deal with this code:
case 'png':
if (!function_exists('imagecreatefrompng')) {
$this->processed = false;
$this->error = $this->translate('no_create_support', array('PNG'));
} else {
echo $this->file_src_pathname;
echo $this->log;
echo $this->error;
echo $image_src = #imagecreatefrompng($this->file_src_pathname);
if (!$image_src) {
$this->processed = false;
$this->error = $this->translate('create_error', array('PNG'));
} else {
$this->log .= '- source image is PNG<br />';
}
}
break;
The #imagecreatefrompng($this->file_src_pathname) function is that piece of code where my code breaks. It doesn't output anything after that code, except when I comment it out. I have already changed my memory limit to 256M and the file upload to 64M. The file name is set. I have no idea why it only breaks my code when it has to deal with big files. Do any of you guys have an idea?
The code on the file upload page is:
//form processing
include('../includes/class.upload.php');
$files = array();
foreach ($_FILES['my_field'] as $k => $l) {
foreach ($l as $i => $v) {
if (!array_key_exists($i, $files))
$files[$i] = array();
$files[$i][$k] = $v;
}
}
foreach ($files as $file) {
if(!empty($file)){
$handle = new Upload($file, 'nl_NL');
if (!file_exists("../classified_images/$adid"))
mkdir("../classified_images/$adid", 0777);
$tag_code_p = generatePassword(25);
if ($handle->uploaded) {
$oriname = $handle->file_src_name;
$handle->mime_magic_check = true;
$handle->allowed = array('image/*');
$handle->image_convert = 'jpg';
$newname = $adid."_big_".$tag_code_p;
$handle->file_new_name_body = $newname;
$handle->image_resize = true;
$handle->image_ratio_fill = true;
$handle->image_y = 600;
$handle->image_x = 800;
$handle->image_background_color = '#FFFFFF';
// // now, we start the upload 'process'. That is, to copy the uploaded file
// // from its temporary location to the wanted location
// // It could be something like $handle->Process('/home/www/my_uploads/');
$handle->Process("../classified_images/");
// we check if everything went OK
if ($handle->processed) {
$handle->image_convert = 'jpg';
$newnamesmall =$adid."_small_".$tag_code_p;
$handle->file_new_name_body = $newnamesmall;
$handle->image_resize = true;
$handle->image_ratio_fill = true;
$handle->image_y = 94;
$handle->image_x = 125;
$handle->image_background_color = '#FFFFFF';
// now, we start the upload 'process'. That is, to copy the uploaded file
// from its temporary location to the wanted location
// It could be something like $handle->Process('/home/www/my_uploads/');
$handle->Process("../classified_images/");
$handle->clean();
//inserten in database
$sql_foto_insert = "insert into photos
( adid, photosmall, photo)
values
('$adid', '$newnamesmall.jpg','$newname.jpg')";
$foto_result = mysql_query($sql_foto_insert);
// everything was fine !
//$msg .= $oriname.' '.LANG_FOTOS_SAVED.'<br />';
$allok = 1;
} else {
// one error occured
$msg .= $handle->error . '<br />';
}
}

This can be caused by configuration issues. Since this happens for files > certain size, I feel it can be related to php ini settings.
Check upload_max_filesize and post_max_size in your php ini settings. These could be set to 4MB. You can increase these values to make it work.
If you are in a shared hosting environment where you cannot edit php ini, you can add them to .htaccess file like so:
php_value upload_max_filesize 7M
php_value post_max_size 7M
Also you should remove the # from your function to see what the error is. Suppressing errors by adding # is a bad idea while developing/debugging.
Additionally, if you are in a local machine or own dev server, open the apache error_log and check the last few lines to see what the error is. If you are on a third party server, most control panels provide an interface to view the error log.

Related

Yii missing file after send via form

i'm new in phbp and yii and i have a pronblem with sendind file, i'm using kartik\file\FileInput widget without model and i send to yii controller, where i can get my file from $POST and in first time i used move_uploaded_file with linkt to my file on tmp. The first idea with move doesnt work, i wouldnt find my file on disk, i know is systemd, but i change my tmp folder in php.ini but the file from form doesnt show in this place. This is my conbtroller
$output = "";
$modelZalaczniki = new DelegacjeZalacznikiSearch();
$modelZalaczniki->d_add = date('Y-m-d H:i:s');
$modelZalaczniki->u_add = Yii::$app->user->identity->id;
if (empty($_FILES['file'])){
echo json_encode(['error'=>'Nie znaleziono plik.w.']);
return;
}
$files = $_FILES['file'];
$success = null;
$paths = [];
$fileNames = $files['name'];
if(!file_exists('uploads')){
mkdir('uploads', 0750, true);
}
if(!file_exists('uploads'.DIRECTORY_SEPARATOR.'delegacje')){
mkdir('uploads'.DIRECTORY_SEPARATOR.'delegacje', 0750, true);
}
if(!file_exists('uploads'.DIRECTORY_SEPARATOR.'delegacje'.DIRECTORY_SEPARATOR.'pliki')){
mkdir('uploads'.DIRECTORY_SEPARATOR.'delegacje'.DIRECTORY_SEPARATOR.'pliki', 0750, true);
}
if(!file_exists('uploads'.DIRECTORY_SEPARATOR.'delegacje'.DIRECTORY_SEPARATOR.'pliki'.DIRECTORY_SEPARATOR.$delegacja_id)){
mkdir('uploads'.DIRECTORY_SEPARATOR.'delegacje'.DIRECTORY_SEPARATOR.'pliki'.DIRECTORY_SEPARATOR.$delegacja_id, 0750, true);
}
for($i = 0; $i < count($fileNames); $i++){
$ext = explode('.', basename($fileNames[$i]));
$hashName = md5($fileNames[$i]);
$target = 'uploads'.DIRECTORY_SEPARATOR.'delegacje'.DIRECTORY_SEPARATOR.'pliki'.DIRECTORY_SEPARATOR.$delegacja_id.DIRECTORY_SEPARATOR.$hashName;
// if(file_exists($target)){
// $success = true;
// break;
// }
if(move_uploaded_file($files['tmp_name'][$i], $target)){
$success = true;
$paths[] = $target;
$modelZalaczniki = new DelegacjeZalaczniki();
$modelZalaczniki->delegacja_id = $delegacja_id;
$modelZalaczniki->d_add = date('Y-m-d H:i:s');
$modelZalaczniki->u_add = Yii::$app->user->identity->id;
$modelZalaczniki->sciezka = $target;
$modelZalaczniki->nazwa = $fileNames[$i];
$modelZalaczniki->typ = $ext[1];
$modelZalaczniki->size = $files['size'][$i];
if ($modelZalaczniki->validate()){
$modelZalaczniki->save();
}
}else{
$success = false;
break;
}
Every things work fine but i cant move file to my folder, aha, file is create but in this file is linkt to yii documentation.
Yii 2.0 is great at uploading files. No need for move_uploaded_file.
Take a look at the documentation, and specifically the UploadedFile::getInstance() method.
Useful examples which are relevant to what you're doing are here:
https://www.yiiframework.com/doc/guide/2.0/en/input-file-upload
In the above code, when the form is submitted, the yii\web\UploadedFile::getInstance() method is called to represent the uploaded file as an UploadedFile instance. We then rely on the model validation to make sure the uploaded file is valid and save the file on the server.

Upload fails "move uploaded file"

First off, the upload folder is given 777, and my old upload script works, so the server accepts files. How ever this is a new destination.
I use krajee bootstrap upload to send the files. And I receive a Jason response. The error seems to be around move uploaded file. I bet it's a simple error from my side, but I can't see it.
<?php
if (empty($_FILES['filer42'])) {
echo json_encode(['error'=>'No files found for upload.']);
// or you can throw an exception
return; // terminate
}
// get the files posted
$images = $_FILES['filer42'];
// a flag to see if everything is ok
$success = null;
// file paths to store
$paths= [];
// get file names
$filenames = $images['name'];
// loop and process files
for($i=0; $i < count($filenames); $i++){
$ext = explode('.', basename($filenames[$i]));
$target = "uploads" . DIRECTORY_SEPARATOR . md5(uniqid()) . "." . array_pop($ext);
if(move_uploaded_file($_FILES["filer42"]["tmp_name"][$i], $target)) {
$success = true;
$paths[] = $target;
} else {
$success = false;
break;
}
}
// check and process based on successful status
if ($success === true) {.
$output = [];
$output = ['uploaded' => $paths];
} elseif ($success === false) {
$output = ['error'=>'Error while uploading images. Contact the system administrator'];
// delete any uploaded files
foreach ($paths as $file) {
unlink($file);
}
} else {
$output = ['error'=>'No files were processed.'];
}
// return a json encoded response for plugin to process successfully
echo json_encode($output);
?>
I think field name is the issue. Because you are getting image name with filer42 and upload time, you are using pictures.
Please change
$_FILES["pictures"]["tmp_name"][$i]
to
$_FILES["filer42"]["tmp_name"][$i]
And check now, Hope it will work. Let me know if you still get issue.
The error is not in this script but in the post.
I was using <input id="filer42" name="filer42" type="file">
but it have to be <input id="filer42" name="filer42[]" type="file" multiple>
as the script seems to need an arrey.
It works just fine now.

php file upload returning error 6 even though tmpdir is set

php file upload returning error 6 even though tmpdir is set in php.ini and all paths are correct. My code is too long to be posted but here's the part where it handles the file
$uploaddir = __DIR__ . '/img/';
echo $_FILES['navpic']['error'];
if($_FILES['navpic']['size'] != 0) {
$navpic = $uploaddir . "navpic.png";
if($_FILES['navpic']['size'] < 1000000) {
if (move_uploaded_file($_FILES['navpic']['tmp_name'], $navpic)) {
$navpicupload = 0;
} else
$navpicupload = 1;
} else {
$navpicupload = 2;
}
} else {
$navpicupload = 1;
}
//edit I also have permission to write in both directories so thats neither the problem
:/

memory limit and slow upload using class upload php

i am creating upload image using class upload from https://www.verot.net/
my problem: output image 2 (real photo and edit-resize photo).
all work but it wasting time while processing upload.
and when i check size real photo before and after upload, size not same.
$counter=1;
foreach ($files as $file) {
$handle = new Upload($file);
if ($handle->uploaded)
{
$handle->Process("real");
$handle->dir_chmod = 0777;
//$handle->jpeg_quality = 85;
//jenis file yang diijinkan
$handle->allowed = array('image/*');//VALIDASI UPLOAD GAMBAR
//$handle->image_resize = false;
/*----first-START----*/
if ($handle->processed) {
${'pathPhotoReal'.$counter} = 'real/'.$handle->file_dst_name;
echo ${'pathPhotoReal'.$counter}."<br>";
${'namaFoto'.$counter} = $_POST['text'.$counter];
echo ${'namaFoto'.$counter}."<br>";
}
else {
// one error occured
echo ' Error: ' . $handle->error . '';
}
/*----first-FINISH----*/
// we now process the image a second time, with some other settings
//$handle->allowed = array('image/*');
$handle->image_ratio = true;
$handle->image_ratio_fill = true;
$handle->image_resize = true;
$handle->image_x = 300;
$handle->image_y = 300;
$handle->Process('edit');
/*----second-START----*/
if ($handle->processed) {
${'pathFotoEdit'.$counter} = 'edit/'.$handle->file_dst_name;
echo ${'pathFotoEdit'.$counter}."<br>";
}
else {
// one error occured
echo ' Error: ' . $handle->error . '';
}
/*----second-FINISH----*/
$counter = $counter + 1;
//$handle->clean();
else {
// if we're here, the upload file failed for some reasons
// i.e. the server didn't receive the file
echo ' File not upload ' . $handle->error . '';
echo($handle->log);
}
}
in my last code using else{echo ' File not upload ' . $handle->error . '';}
it always show, inside folder pathPhoto have saved photo inside. i think upload success.
try changing the folder permissions from filezilla.
With this you can know if the server allows you to make those persmisos

Retrieve filename extension for multiple uploaded files (Class Upload, PHP)

Language: PHP / Using Class Upload by Colin Verot
About: Multiple Uploading
The code below already uploads the files fine, it works...
PROBLEM: I am having trouble figuring out how to get the filename extension.
(In a comment below, I have specified where my problem area is...)
// CONNECTION TO DATABASE HERE...
// INCLUDE UPLOAD CLASS LIBRARY
include (dirname(__FILE__).'/lib/class.upload.php');
$files = array();
foreach ($_FILES['fileupload'] as $k => $l)
{
foreach ($l as $i => $v)
{
if (!array_key_exists($i, $files))
$files[$i] = array();
$files[$i][$k] = $v;
$imagename = $_FILES['fileupload']['name'];
}
}
foreach ($files as $file) {
// THIS IS MY PROBLEM AREA, GETTING FILE EXTENSION
$ext=strchr($imagename,".");
$generate_name = rand(100,99999);
$generate_name_extra = rand(200,9999);
$filenamex = "PHOTO_".$generate_name.$generate_name_extra."_".time();
$filenamex_thumb = $filenamex."_thumb";
// COMPLETE FILENAME WITH EXTENSION
$filename = $filenamex.strtolower($ext);
$handle = new upload($file);
if ($handle->uploaded) {
///// 1 ////////////////////////////////////////////////////////////////////
$handle->file_new_name_body = $filenamex_thumb;
$handle->image_resize = true;
$handle->image_x = '300';
$handle->image_ratio_y = true;
$handle->jpeg_quality = '100';
// ABSOLUTE PATH BELOW
$handle->process($absoRoot.'covers/thumbs/');
if ($handle->processed) {
// SUCCESSFUL RESPONSE
}
else
{
// FAILED RESPONSE
}
}
}
And the webform is:
<form method="post" action="upload.php" enctype="multipart/form-data">
<input name="fileupload[]" id="fileupload" type="file" multiple>
I really pretty much need the file extensions to serve the files correctly online, but I can't spot where to find it. I have tried using: $files[$i][$k] instead of $imagename in my specified problem area above, as well as $file and other possible solutions, but I can't spot which one's going to give me the filename with extension.
Hopefully someone could point it out. Thank you for your time and assistance!
$ext=array_pop(explode('.', $imagename));
The function you're looking for is pathinfo().
$pathdata = pathinfo($filename);
$extension = $pathdata['extension'];

Categories