Problem with Flash PHP upload - php

I am using a script to upload photo (written by my own) which allow user to select more than 1 files, using Flash.
When user click upload, as3 will post the upload file to upload.php, resize it, and save it one by one.
The problem:
In production server, if I am uploading many photos and the photos size are very large, >2MB, the uploading progress just stuck in halfway, only the first few photos successfully uploaded.
What I have found out:
If I tried with smaller size photos, or if I disabled the resizing script in upload.php, there are no problem at all.Furthermore, the script work fine in my localhost with newer processor compared to the problematic server with older model)
The resize script is simple. It just check whether the image resolution is large, and use imagecopyresampled() to resize the image if needed.
I tried to unset image resource in upload.php to free up memory but it doesn't help.
What else could be the problem?

If I remember right, there is a 2mb upload limit in php, you can change it in the php.ini file.

I solved the problem.
It happens when I use code like this in AS3:
for(var i:Number = 0; i< fileList.length; i++) {
fileList[i].upload(new URLRequest(param.uploadURL+"&sessionid="+param.session_id));
}
The code seems like pushing upload.php script in very tedious way.
I made changes to both my AS3 and JS, so that JS call to AS3 upload() function only everytime a file upload is completed (from event listener EVENT.COMPLETE)
By making so, AS3 wont call for upload.php synchronously for multiple files. Instead, it wait for a file completed the upload, then call for another upload for the next file.

Related

Try upload videofile

I use Laravel 5.4 and I try upload video file. Image file upload successfully.
$video = Request::file('video_file')) {
$fullName = 'videos/'.uniqid().time().'.'.$video->getClientOriginalExtension();
Storage::disk()->put($fullName, $video);
But it didn't work. When I try get information about file - size = 0
What I do wrong?
There’s a limit on the amount of data you can send in a POST request. If you exceed that limit, PHP will return zero as the size of the file.
Instead, you’ll need to upload the file in chunks. If you’re using something like Amazon Web Services, they have a JavaScript SDK that will handle multi-part uploads for you: http://docs.aws.amazon.com/AWSJavaScriptSDK/latest/AWS/S3.html#upload-property
Check out http://php.net/manual/en/ini.core.php#ini.post-max-size
PHP has a default max_post_size of 8mb I believe, anything larger than that will not go through. There is also a upload_max_filesize that you may want to check out. I'm not sure if increasing the limit is the correct answer here, as I'm not sure what complications that would bring. When we upload large Files we use blue imp's file uploader: https://github.com/blueimp/jQuery-File-Upload
It's pretty straight forward and automatically chunks the uploads for you.

How do I only show pictures in PHP once they're done uploading?

I'm uploading an image using WinSCP, which gets sent to a web server using PHP.
Then the site refreshes automatically every X seconds and notices that a new image file is present, and displays it to the user.
However, during the loading time, it shows the following image, which doesn't look very nice:
http://s10.postimg.org/fwz0ok16h/imageupload.png
How can I ensure it only shows the image after it is completely uploaded and ready?
Is there maybe some sort of "pre-loader" or fade effect you could use in PHP that really only shows the picture once it's done? It needs to be PHP because Javascript can't find out the exact image name. Here's how I currently display the image:
foreach($files as $key => $value)
{
if($count >= 1)
break;
echo '<th><img id="image" height="250px" width="250px" src="files/'."$key".'"><br />'."$value".'</img></th>';
$count++;
}
Open the Image using
$png = #imagecreatefrompng('stamp.png');
// Or:
$jpg = #imagecreatefromjpeg('photo.jpeg');
If it doesn't fail, the image is complete. If it fails you either don't have the GD lib installed and enabled or the image is corrupt / incomplete.
But it is the best way to check if the Image upload is complete using Javascript event handlers, like onreadystate attached to the upload, and only refresh the page when that event is triggered.
Further links:
jQuery: Check if image exists
Taking control of image loading
Propably the best one: Is there any way to have PHP detect a corrupted image?
Before outputting a file, check its filemtime. If that modification time is less than a few seconds ago, you can assume that it's still being uploaded and skip over it.

Multiupload of files using flash uploader and php and saving into mysql database

I want to develop a backend admin solution to save images for a post using some multiupload flash uploader. I mean that kind o f uploader, where when you click on browse and than in the open dialogue box you can choodse mulitple files using CTRL plus LEFT MOUSE CLICK).
I would like to save every new image to the database. With saving I mean, creating a new row for every item(image) in my table called images:
1.id (automatically increasing)
2.file_name
3.user_who_uploaded_that_book
I would like also to limit the number of files that can a user uplaod (e.g. max 20 files) somewhere in the config file.
Personally I'm a fan of Plupload which include a nice set of examples on how to set it up for multiple files upload. It also includes an upload.php script, as example for backend setup.
HTML4 doesn't support multiple selection of files, so you need to rely on either HTML5 or extension (like flash or silverlight) for that. Plupload supports all of the above, so it should save you some legwork.
I think you've got 2 basic questions here:
How to select local files for upload via flash?
How to send multiple files to server as POST?
For #1 You need to use flash.net.FileReference.
For #2 There's no built-in 'Multipart' loader in flash, which I'm pretty sure is what you need. The best one I've found is by this genius developer named Eugene: http://blog.inspirit.ru/?p=198. This works great, though personally had I've some issues with the onComplete handler in IE8 & IE9. I'm sure it's IE's fault and not Eugene's. I worked around this my listening for the HTTPStatusEvent event like so:
var ml:MultipartURLLoader = new MultipartURLLoader();
ml.addEventListener(HTTPStatusEvent.HTTP_STATUS, onHTTPStatusEvent);
ml.addVariable('Content-Type', "image/png");
ml.addFile(pngStream, filename, "file", "image/png");
ml.load('http://...');
function onHTTPStatusEvent( event: HTTPStatusEvent ){
if(stat == 0 || (stat >= 200 && stat < 205)){
//upload success
}else{
//some kinda error
}
}
(This should work with multiple addFiles(_);)
I'm also listening for the standard events:
ml.addEventListener(Event.COMPLETE, uploadComplete);
ml.addEventListener(ProgressEvent.PROGRESS, uploadProgress);
But haven't been able to get PROGRESS to work at all, and COMPLETE doesn't fire on IE... Anyone know any alternatives that work on IE? Eugene's code is working for me now, but I don't think I can add a "uploading..." progress bar as it is... which would be cool.

PHP test for corrupt (but apparently fine) images

I have a simple image upload script that uses SimpleImage.php
(http://www.white-hat-web-design.co.uk/blog/resizing-images-with-php/)
to resize and save 2 copies of an uploaded image.
There isn't a massive amount of validation, just checking that it exists and that the file extension is fine, and also an exif_imagetype(); call.
This has worked with no problems so far until I tried to upload a seemingly normal jpeg which turned out to be invisibly (and untestably?) corrupt. There was something not right about it, but I know very little about image corruption - it looked fine and opened no problem on anything, but when I tried to save a scaled copy in my script I got a white page.
The problem is definitely that specific image, I've tested exhastively with other images both from my local stock and from stock image sites, and only that one image breaks it.
I resized a copy using Photoshop (the predicted file size thingy gave me some wierd numbers - 45meg for top quality jpeg) and that uploaded with no issues.
So my question is, how do I test for this?
The image in question is here: http://chinawin.co.uk/broken.jpg //beware, 700k
notes: I've tested with similar resolutions, image sizes and names, everything else worked apart from this image.
UPDATE:
Through trial and error I've narrowed down where the script breaks to the line where I load the image into a var for SimpleImage. Strangely this is the second line that does so (the first being to create the large copy, this one to create a thumbnail).
Commenting it out means the rest works ok... perhaps some refactoring will avoid this problem.
2nd Update:
Here's a snippet of code and some context from the line that fails:
//check if our image is OK
if ($image && $imageThumb)
{
//check if image is a jpeg
if (exif_imagetype($_FILES[$k]['tmp_name']) == IMAGETYPE_JPEG)
{
list($width, $height, $type, $attr) = getimagesize($_FILES[$k]['tmp_name']);
//echo 1;
$image = new SimpleImage();
//echo 2;
$image->load($_FILES[$k]['tmp_name']);
//echo 3;
$imageThumb = new SimpleImage();
//echo 4;
//this next line topples my script, but only for that one image - why?:
$imageThumb->load($_FILES[$k]['tmp_name']);
//echo '5<br/><br/>-------<br/>';
//do stuff, save & update db, etc
}
}
Final edit:
Turns out my script was running out of memory, and with good reason - 4900x3900 image with 240 ppi turns out to be around 48 meg when loaded into memory, twice - so I was using probably > 90meg of ram, per image.
Hats off to #Pekka for spotting this.
Refactoring the script to only have the image loaded once, and then this variable used instead of it's sibling, fixed my script. Still having (different) issues with upoading larger (2.5meg) images but this is for another question.
This is most likely a memory issue: Your JPG is very large (more than 4000 x 4000 pixels) and, uncompressed, will indeed eat up around 48 Megabytes of RAM.
Activate error reporting to make sure. If it's the reason, see e.g. here on what to do: Uploading images with PHP and hitting the script memory limit

Keeping uploaded images in tmpfolder for resizing

I am working on an upload script that also resizes/rescales an image.
Currently it is working by uploading the image, moving to the upload dir (site.com/upload) and then resizing, and afterwards deleting the original again...
Now my question is:
Can I do this without moving the original to the upload dir and even better, also keeping the new file in tmp so I the user can afterwards confirm the image (so If they don't want it and just hit the 'back' button it won't stay in the upload dir.)
Current code:
move_uploaded_file($_FILES['file']['tmp_name'], 'resize-upload/'.$_FILES['file']['name']);
$filename=$_FILES['file']['name'];
$Imagick=new Imagick();
$Imagick-> readImage('resize-upload/'.$filename);
$Imagick-> scaleImage(200,200,auto);
$Imagick-> writeImage('resize-upload/resized-'.$filename);
unlink('resize-upload/'.$filename);
You can't. You don't have the access to the image if you haven't moved it yet. But you can generate more than one thumbnail at a time from same image/object and if you are storing/keeping the original also then you don't need to unlink it either.
Another way would be to use flash and resize the image on the client side and then only save the resized image.

Categories