Progress Bar File Upload - Without AJAX - php

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

Related

PHP: File is uploaded but it does not get moved from the temporary directory. How do I resolve it?

I am trying to upload an image to server (I am using Google Cloud Virtual Machine) that the PHP gets from a Python script (I intend to retrieve the images from a Raspberry Pi camera sensor, but for now, I am sending a placeholder image to test the functionality of my script) that sends the image along with other parameters through a POST request. However, while the image does get uploaded, it does not get moved to a new location as it is shown in the Output I have provided below along with the code.
A question found here: php uploading a file says it is working but does not actually upload a file does have a similar problem, however the suggested solution there did not help me.
Here's the code that implements the file upload system.
PHP Upload File code:
//Upload the image
$uploadDir = "/home/username/directory/";
$uploadFile = $uploadDir . basename($_FILES["myimage"]["name"]);
if(is_uploaded_file["myimage"]["tmp_name"])
{
echo "File has been UPLOADED\n";
if(move_uploaded_file($_FILES["myimage"]["tmp_name"],$uploadFile))
{
echo "File has been MOVED";
}
else
{
echo "File has NOT been MOVED";
}
}
else
{
echo "File has NOT been UPLOADED";
}
Python code which sends a request to the PHP above:
import requests
def postData(directory):
activity = "Someone walked in"
url = 'http://35.198.114.146/index.php/sensor_readings/postdata?activity=' + activity
image = {'myimage': open(imagePath,'rb')}
r = requests.post(url,files=image)
print(r.text)
imagePath = 'cam_images/test.png'
postData(imagePath)
Output:
File has been UPLOADED
File has NOT been MOVED
I have been stuck on this problem for weeks and I am not sure on what to do. Any kind of help is appreciated. Should you need more details or code, feel free to ask me.
Thank you in advance.

Struggling To Use PyroCMS Files Library To Upload Multiple Files

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.

File uploading only working with some images (class.upload.php)

I've tried the native way of uploading images with PHP, but my friend suggested me to the class.upload.php library, which still had the same results as before. I can only upload certain images without them having that little icon that means that the browser can't find the image, but what's weird is that when I download the "invalid" images to my computer, they're fine. About 50% of images actually do work, but the rest just show the appropriate size that they should have but no image. Here is my code (my html form has a file input type called filename:
$handle = new upload($_FILES['filename']);
if ($handle->uploaded)
{
if ($file_src_size < 20000)
{
$handle->file_new_name_body = "test";
$handle->image_convert = 'jpg';
$handle->process('/');
echo "<img src = \"test.jpg\" />";
}
else echo "Files must be 20 kb or under";
}
else echo "Upload failed, please try again";
The fact that you can download the images and re-display locally them suggests that the problem is not with the upload script. An alternative problem could be that the images are not properly saved for web viewing. For example, just because a file ends in .jpg and can be viewed locally does not mean that it is recognizable by a browser. A CMYK jpeg cannot be viewed by many browsers.
You need to debug your code as something is going wrong, and only someone with access to your computer can tell you why.
1) The library you're using has error detection code
if ($handle->processed) {
echo 'image resized';
$handle->clean();
} else {
echo 'error : ' . $handle->error;
}
2) You should be able to look at the processed image data, and figure out why the browser is not able to parse it as an image. If you can give a URL for an invalid image I would be able to tell you what's wrong with it.
3) Writing files to the root directory by calling "process('/');" is bad. You should make a directory to hold files that come from other people, to avoid having potential problems when some uploads an image called 'index.php'.

PHP is_uploaded_file fails for iOS app

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

Getting a file from a form in Zend

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.

Categories