I'm trying to upload a file to Amazon S3 using Zend. Everything is working except I can't get access to the file in the POST[] array.
Is there anyway I can easily take the file from the form. All the documentation examples only show you how to do this when uploading a file to your local file system.
It is quite straight forward once you get the hang of it.
$form->image->setDestination('path/to/images');
if($form->isValid($_POST)){
if($form->image->isUploaded()){
if($form->image->receive()){
// For example, get the filename of the upload
$filename = $form->image->getFilename();
}
} else {
// Not uploaded
}
} else {
// Not valid
}
Note that in my example image is the name of the upload element.
A small correction on the above it should be getFileName with a capital N.
$filename = $form->image->getFileName();
If you are uploading a file to your PHP script from a form, and then intend to upload that file to S3, you're looking in the wrong superglobal.
File uploads live in $_FILES, not $_POST. Check out the PHP documentation on handling file uploads for information on how to use it best.
Related
I am using upload php class from this site: http://www.verot.net/
Image upload is working fine. But when I try to upload csv file it throughs an error:
getimagesize(): Read error! [APP\Vendor\class.upload.php, line 2423]
Here is my code:
$upload = new Upload($file['file']);
$upload->no_script = false;
$upload->allowed = array('application/msword');
$upload->file_new_name_body = 'data';
$upload->process($this->target_path);
if (!$upload->processed) {
$msg = $this->generateError($upload->error);
$this->Session->setFlash($msg);
return $this->redirect($this->referer());
}
Here $file has the all info of attached file.
If I try to upload an image it works fine but when I try to upload a csv file it shows error. I set the mime-type. But no luck. Anyone have this experience. Or is there any plugin like verot.net to upload file. Any idea will be appreciated
This tutorial guides you trought file uploading process:
W3Schools: File Upload
File uploading is not very hard, so I suggest you to try learning it.
Hope this helps!
Your class file is meant to upload or resize image files only.
It will not work for other extensions like .csv, .txt or any other text file or non image file.
I was searching answer to your same question and got that this class should work fine with any other file upload . You just need to set the following for all files other than image
$upload->mime_getimagesize = false;
This will not throw the error you mentioned for non image files
I'm writing the PHP/webserver side of an image upload for an iOS app. Using an html file I can upload an image to my script just fine. I've even written a Perl LWP script to post an image with no problems.
When the iOS app uploads an image it fails when I call is_uploaded_file. Sending back the $_FILES var in the json response show us what we expect for a file upload. Also I do a file_exists on the tmp_name and that fails as well.
Is there anything else I can look at in the request to determine what is going wrong? I'd like to be able to indicate what's wrong with the post request.
Here's the block of code where it stop processing the image upload:
//Check for valid upload
if(!is_uploaded_file($_FILES['photo']['tmp_name'])) {
$this->errors['upload_2'] = $photoErrorString;
$this->errors['files'] = $_FILES;
$this->errors['image_uploaded'] = file_exists($_FILES['photo']['tmp_name']);
error_log("uploadPhoto: upload_2");
return false;
}
As far as i know you cannot upload a image or photo from iOS app directly using PHP scripts.
You have to send 64 bit encode from the iOS app to the script responsible for the file upload as a simple POST. Then that script will firstly decode your photo's string and then PHP script will create the image from the string only to upload.
Code will be something like:
$filedb = $path_to_dir.$file_name_of_the_photo;
$profile_pic = $_POST['profile_pic'];
$profile_pic= base64_decode($profile_pic);
if(($img = #imagecreatefromstring($profile_pic)) != FALSE)
{
if(imagepng($img,$filedb))//will create image and save it to the path from the filedb with the name in variable file_name_of_the_photo
{
imagedestroy($img);// distroy the string of the image after successful upload
// do other updates to database if required
}
else //cannot create imagepng Error
{
//do what required
}
}
Hope this will work.
Linked below is a really nice NSData base64 category.
If you're unfamiliar with categories in Obj-C, they're basically stock objects that we know and love with some added methods (in this case, base64 encoding/decoding).
This class is really simple to drop into an existing project.
Enjoy.
http://cocoawithlove.com/2009/06/base64-encoding-options-on-mac-and.html
Is there any way to limit the type of file that can be uploaded using an HTML form via PHP? For example, I want to limit uploads to only .pdf files.
Server side
Yes use superglobal $_FILES. look at this documentation page : PHP.net
It will get through basics, and then just look for file type, which is what you need to check with.
Client side
There are many ways to do this, but you can achieve this simply :
if((document.form1.upload.value.lastIndexOf(".jpg")==-1) {
alert("Please upload only .jpg extention file");
return false;
}
Yes. just look in the $_FILES array and read the file type.
Is there any way for a php script to choose the name for a file after it's been uploaded by the user using an HTML form? I am wanting to allow users to upload an avatar for their account and would like it named with their userid instead of whatever the name of it is on their computer. I'm using a basic HTML upload form which only allows jpegs and png files with a 10MB file limit, similar to the file upload code give on http://www.w3schools.com/php/php_file_upload.asp Any help you can give will be greatly appreciated.
Put the desired filename in the second argument of move_uploaded_file().
You can specify the filename when using move_uploaded_file(), otherwise you can rename() the file.
$userid = 5; // say you fetch it from database
$ext = explode("\/",$_FILES["file"]["type"]); //extract the file extension
move_uploaded_file($_FILES["file"]["tmp_name"], "upload/".$userid.$ext[1]);
UPDATE:
I think you don't need to extract file extension.
move_uploaded_file($_FILES["file"]["tmp_name"], "upload/".$userid);
I am facing the task of having to upload a snapshot to the server. But I don't want the user to download the image to their computer.
I have explored a few solutions of generating an image serverside with PHP, but they all seem to use a method where the server sends the image to the user.
See for instance: http://mattkenefick.com/blog/2008/11/06/saving-jpegs-with-flash/
I'm wondering if it's possible to save $GLOBALS["HTTP_RAW_POST_DATA"], which in that example contains the ByteArray sent by Flash, to the server as an image file....
Use php code that is along these lines to save the contents of $GLOBALS["HTTP_RAW_POST_DATA"]
// untested code
$imageBytes = $GLOBALS["HTTP_RAW_POST_DATA"]
// in real code you better create a new file for every upload :-)
$file = fopen("uploads/test.jpg", "w");
if(!fwrite($file, $imageBytes)){
return "Error writing to file: $file";
}
fclose($file);