简体   繁体   中英

Preview images with AJAX image upload

I have created an ajax image upload system. When the user selects the image he wants to upload, the image is checked for security reasons and then uploaded to a file folder. The image is then displayed through the path of that file folder on ajax callback. The problem is that I want the image to be uploaded to the file AFTER the user has clicked a button. This would force me to rely on the temporary path file of the image, $_FILES['file']['tmp_name'] as the source of the image to be displayed. However, that doesn't work.

My question is this. If I can't use the temporary file path as the image source for the uploaded image, what would be my other options? I guess I could when the submit button is clicked move the file AGAIN and delete the old image, but there is the possibility the user might upload the file, exit the browser, and leave the image in the folder forever.

You can make a temp directory and move the uploaded file there, then if the user does not want to upload it after he see the preview you delete the image. You can't use the php's temp file because it lasts until the script finished it's execution.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM