How to get current uploaded bytes in php? - php

How can I log and get the current bytes being uploaded from a html form to a PHP script? I want to later store the current progress in a session or log so that another AJAX script can retrieve it. Here is my current code to read the current bytes but It doesn't work.
$file_type = $_FILES['Filedata']['type'];
$file_name = $_FILES['Filedata']['name'];
$file_size = $_FILES['Filedata']['size'];
$file_tmp = $_FILES['Filedata']['tmp_name'];
$filePointer = fopen($_FILES['Filedata']['tmp_name'], "rb");
$rr=0;
if ($filePointer!=false){
while (!feof($filePointer)){
$fileData = fread($filePointer, 4096);
$rr =$rr+strlen($fileData);
// Process the contents of the uploaded file here...
$_SESSION['fname']=$rr;
}
fclose($filePointer);
}
move_uploaded_file ($file_tmp, "files/".$file_name);

You can not access the uploaded bytes that way as the script doesnt get called till the file is already uploaded.
Your server needs to have APC, or similar pecl packages installed
http://devzone.zend.com/1812/using-apc-with-php/
Or by using the HTML5 file api
http://www.matlus.com/html5-file-upload-with-progress/
The HTML5 is better as you dont have to change anything on the php side just setup the scripts to receive the files, but would require that the user's browser support the HTML5 file api.

There is a PECL package: http://pecl.php.net/package/uploadprogress
Else check this:
http://devpro.it/upload_progress/
http://www.johnboy.com/php-upload-progress-bar/
You have to find the correct file in tmp folder and measure the size. This size must be shown on a page which is crawled via AJAX every some seconds. The problem is how to match the correct session and file. But it is not impossible...

Related

iOS NSStream to PHP uploader?

I'm working on uploading a large file to my server. I have the iOS code that sets up an input and output stream. I can see that it successfully uploads to my videoUploader.php file because I have it making a response with the temporary file size and location. However, it executes the code, or so it seems before it gets the rest of the file. I cannot find any where online a full example of the server side handling.
$upload_directory = 'uploads/';
$temp_file = $_FILES['uploaded']['tmp_name'];
$filename = "testme.mp4";//basename($_FILES['uploaded']['name']);
$target_location = $upload_directory . $filename;
$size = $_FILES['uploaded']['size'];
move_uploaded_file($temp_file, $target_location);
All my files that it "moves" are 0 kb. It seems like there needs to be a way for me to tell php to wait until the temporary file is complete and then execute this code. I am an extreme novice at php, help please!

using php to check if a file is a kml file

I am using move_file_upload on my server side in php so that client can allow users to upload their kml files to the server. I know that in order to check an image , in php I can use $check = getimagesize(file) but what would be the equivalent for a kml file check ?
I donot want to just check the extension of the file. I wish to know if infact the file is a valid kml file or not. If I only check the extension, someone can just post some other malicious file and change its extension to .kml
If you want to see if the file has the extension KML, you can use:
$filename = $_FILES["file"]["name"]; //or however you are getting the filename
$ext = end((explode(".",$filename)));
if($ext!="kml"){
//Extension is incorrect
}
Checking mime content can be helpful.
I am not quite sure what is the correct mime name of kml files but at least with checking in google it should be something as:
mime_content_type ($file) === 'application/vnd.google-earth.kml+xml'
How ever its possible that there are mimes set to 'application/xml' or 'text/xml' so extension validation is required as well ..

getting file information via url in php

like file upload there are
<?php
$_FILES['file']['tmp_name'];
$_FILES['file']['name'];
$_FILES['file']['size'];
$_FILES['file']['type'];
?>
now.
i have a file that is sitting on my other web server, and i want to get the name size and mime type of that file via url.. is this possible?..
i've alreay tried to use this code below. but it doesn't work
$url = "http://mydomain.com/myfile.rar";
filesize ( $url );
mime_content_type ( $url );
You can try native php function get_headers it's very fast way to read file data
You can't do it like this. The information you get when you use $_FILES is meta-information that is sent along with the file (and the size can even be calculated after the file is retrieved).
You cannot get this information like that, but you can download the actual file and inspect the header information to get that information. To do this, read about curl, which allows you to do HTTP requests to another server.
It might be possible to request just the headers, so you get the information without getting the file, which is obviously more efficient.
Another solution is to implement a file-info script on the other server that allows you to get the file info.
So you could request http://mydomain.com/fileinfo.php?file=myfile.rar. In fileinfo.php you can get all the file info of the given file and just echo it.

What's the difference in these ways of determining uploaded file size in PHP?

I came across this piece of code where programmer determines uploaded file size like this:
$file_size = #filesize($_FILES[$upload_name]["tmp_name"]);
AFAIK one can simply do:
$_FILES[$upload_name]["size"];
Are there reasons to use filesize() function over reading file size from $_FILES array?
$_FILES[$upload_name]["size"]
This is populated for you by PHP after receiving the uploaded file.
$file_size = #filesize($_FILES[$upload_name]["tmp_name"]);
This is a wasteful method that goes out and gets the file size from the drive, even though it's already populated inside $_FILES[$upload_name]["size"].

Correctly setting headers so a file can be downloaded via a proxy using PHP

I'm finding it difficult to phrase this question correctly, let me try to explain our problem...
We have an intranet running on Ubunutu box with Apache2/PHP 5.2.4. We have a bit of PHP code that reads a file from a directory that is not publically accessible and output it to the screen (code below):
$file_path = '/home/path/to/filename.gif';
if(file_exists($file_path)){
$output = FALSE;
//File Information
$path_parts = pathinfo($file_path);
$file_size = filesize($file_path);
$file_ext = (isset($path_parts['extension'])) ? strtolower($path_parts['extension']) : null;
$file_name = $path_parts['basename'];
//Sets up the headers
if($file_size > 0){
header('Content-Length: ' .$file_size);
}
header('Content-Disposition: attachment; filename="'.$file_name.'"');
header('Content-Type: application/octet-stream');
//Reads the File
if($file_size > 0){
$handle = fopen($file_path, "r");
$output = fread($handle, $file_size);
fclose($handle);
}
//Outputs the File
echo $output;
}
Inside our network when, browsing to the page that uses this code, the file is downloaded perfectly and quickly...
However, when accessing this page via our Cisco ASA/Proxy/VPN (not sure what to call it) this code locks up the browser, but does eventually download the file...
After a bit of experimenting, after taking out the headers and just echoing the contents of the file to the browser, it prints no problem. However as soon as I add the lines with the headers back into the code it causes the hanging again, but only when accessed via this box..
Anybody come across this problem before or have any idea what we can try to move forward?
Thanks for any advice...
Have you tried eliminating the content-size header entirely? The proxy may be taking that as a firm promise and if the data you're sending ends up being a different size, the proxy may wait for those last few "missing" bytes to show up.
Just as an aside, you should use [readfile()][1] instead of the fopen()/fread()/echo construct you have now.
As it stands now, you're slurping the contents of the entire file into memory and then echoing out. For large files and multiple requests, you'll kill the server with memory starvation. readfile will automatically stream the file in smaller chunks so that memory usage is minimal.
Your proxy obviously have problems with the Content-Type: application/octet-stream. Try setting it to the real MIME-type of each file. You can use the Fileinfo module to find out which MIME-type a certain file is, like this:
//You may need to specify the location of your system's magic file
//See http://php.net/finfo_open for more info
$finfo = new finfo(FILEINFO_MIME);
$mimetype = $finfo->file($file_path);

Categories