Am cross posting this with the PyroCMS forum in the bid to reach a wider audience for help as I just can't seem to fathom it.
When I try to upload multiple files using the PyroCMS Files library I run into problems. I can seemingly get files to upload singularly when calling the library and I can also get files to upload when bypassing the library and testing via move_uploaded_file. I've created the following (slightly stripped down) code in my controller:
public function upload($id)
{
// Folder selected or redirect
$id or redirect('admin/mis/resources');
// Run on validation
if ($_FILES)
{
if (count($_FILES['userfile']['name'] > 0))
{
foreach ($_FILES['userfile']['name'] as $file)
{
$upload = Files::upload($id, $file);
}
}
redirect('admin/vle/resources/contents/'.$id);
}
$this->template->build('admin/resources/upload');
}
However when running this code, no file is uploaded. If I output the upload I get the following message:
Array ( [status] => [message] =>
You did not select a file to upload.
I seems that I cannot for the life of me work out how to pass more than one file to Files::upload. I sure I'm missing something. However it's not that I cannot get files to upload in general because if I change the code to this locally for testing purposes my files are uploaded just fine:
if($_FILES)
{
if(count($_FILES['userfile']['name']))
{
foreach ($_FILES['userfile']['name'] as $file)
{
$img = "c:/wamp/www/testupload/files/".$file;
move_uploaded_file($_FILES['userfile']['tmp_name'][$i], $img);
}
}
}
The upload method of the files library is here https://github.com/pyrocms/pyrocms/blob/2.2/develop/system/cms/modules/files/libraries/Files.php#L333
I have tried lots of different things to the point where my head is spinning somewhat now trying get to grips with this. So if anyone can help it'd be appreciated.
The Files library doesn't currently support multiple files uploaded by one element. So it is trying to find a single file from the "userfile" file input instead of an array of files.
This should work:
<form method="post" action="/upload/1" enctype="multipart/form-data">
<input name="userfile" type="file"/>
<input name="secondfile" type="file"/>
</form
public function upload($folder_id)
{
$upload = Files::upload($folder_id, 'Descriptive Name', 'userfile');
$second_upload = Files::upload($folder_id, 'Some Name', 'secondfile');
}
The admin panel of PyroCMS uploads multiple files by using the BlueImp jQuery uploader. Perhaps a JS solution like that would work well for you? That way users get progress bars informing them of each upload's status and they can see when each upload completes.
Related
Am planning to develop a image upload API which need to upload a image to the server location as part of my project. (The usage will be to upload user pics and avatar using an android app)
The API which should be similar to Imgur API in which we can use a post request to upload a binary image to the server.
I searched through multiple questions, all am getting is using multi part request which requires html form submitting. Since my aim is to create a API, html form submitting will be impossible.
Anyway am submitting a sample code which uses html form to upload an image. Can someone show to how can I modify the script to meet my requirement?
<html>
<body>
<form action="photo.php" method="post"
enctype="multipart/form-data">
<label for="file">Filename:</label>
<input type="file" name="file" id="file"><br>
<input type="submit" name="submit" value="Submit">
</form>
</body>
</html>
PHP code
<?php
$allow = array("jpg", "jpeg", "gif", "png");
$todir = 'uploads/';
if (!!$_FILES['file']['tmp_name'] ) // is the file uploaded yet?
{
$info = explode('.', strtolower( $_FILES['file']['name']) ); // whats the extension of the file
if ( in_array( end($info), $allow) ) // is this file allowed
{
if ( move_uploaded_file( $_FILES['file']['tmp_name'], $todir . basename($_FILES['file']['name'] ) ) )
{
// the file has been moved correctly
}
}
else
{
// error this file ext is not allowed
}
}
?>
Some remarks about your server-side code in no particular order:
As you can read at Handling file uploads, the correct way to verify an upload is comparing the ['error'] subkey with UPLOAD_ERR_OK (codes explained here). Don't use ['tmp_name'] for that.
Don't let the end user pick the actual file name on your server. You'd better generate a unique name yourself and keep the display name elsewhere (e.g. a database)
The recommended way to determine a file extension is pathinfo:
pathinfo($_FILES['file']['name'], PATHINFO_EXTENSION)
File extension provided by the user is not a reliable way to determine file type. For pictures, the getimagesize function is often used.
multi part request which requires html form submitting
That's wrong. It requires a properly formatted request (headers and body), period. In fact, the server has no way to know what piece of software was used to generate the request—or if you just typed the bytes yourself in a console ;-)
I'm attempting to get the upload progress from a file upload that I have programmed. The default upload progress (bottom left of browser) comes up and im trying to replicate this.
My PHP uploader works like this:
if (isset($_POST['fileUploadProcess'])) {
define ("filesplace","./voucher-portal/");
if (is_uploaded_file($_FILES['file']['tmp_name'])) {
if ($_FILES['file']['type'] != "application/pdf") {
?>
//refuse anything but pdf
<?php
} else {
$result = move_uploaded_file($_FILES['file']['tmp_name'], filesplace."/$name.pdf");
}
if ($result == 1)
?>
//echo works or not
The uploading process all works mighty fine, however a progress bar is a must and I cant seem to find anything that I can incorporate with my PHP method of file uploading.
NOTE: I'm using this method as I rename the file and place it in a certain directory with database driven vairables
fI have the following code:
$ipaFile= '/path/file.ipa';
$iconFilePath = "Payload/myapp.app/AppIcon40x40#2x.png"; // the pathway to my image file if the ipa file is unzipped.
$iconFile = "AppIcon40x40#2x.png";
$iconSaveFile = '/path/';
if ($zip->open($ipaFile) === TRUE) {
if($zip->locateName($iconFilePath) !== FALSE) {
if($iconData = $zip->getFromName($iconFilePath)) {
file_put_contents($iconSaveFile.$IconFile, $iconData);
}
}
}
This code successfully pulls an image out of an ipa (zipped) file and puts it where I want it to be. The image displays properly in image viewing programs. However, when I want to view the image in a browser, the browser tells me that the image cannot be displayed because it contains errors.
Doing further research, I get that the file is somehow not being unzipped incorrectly. There are a lot of issues regarding images not displaying in browsers, but there are a wide variety of reasons and I'm just not sure which one is mine. I've tested a variety of ways to try and fix the problem (fread method of getting file, using PHP's image functions, using headers, etc) and I can't seem to make it work. Any help would be appreciated, thanks.
For any who will wonder, when Xcode (Apple) compiles an app, it modifies the PNG files within them and a standard browser will not render them as is.
http://echoone.com/filejuicer/formats/ipa
https://theiphonewiki.com/wiki/IPA_File_Format
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
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.