I all,
I'm trying to upload an xls file using the HTML5 API.
I already have a script which is working great for images.
It's also working with an xls file except the fact that I can't open the file using excel once it has been uploaded...
If I compare the size, the orignal file is 251 904 octets while the upload one is 251 911 octets. I don't know if that matter.
Here is what i've done :
Javascript:
reader = new FileReader();
reader.onloadend = function(evt) {
that.file = evt.target.result;
};
reader.readAsDataURL(file); // I've also try with readAsText but it's worse
the file is send using Sproutcore Framework:
SC.Request.postUrl(url).json()
.notify(this, 'fileUploadDidComplete', fileName)
.send({ fileName: fileName, file: file });
PHP:
$httpContent = fopen('php://input', 'r');
$json = stream_get_contents($httpContent);
fclose($httpContent);
$data = json_decode($json, true);
$file = base64_decode($data['file']);
$fileName = substr(md5(time().rand()), 0, 10).$data['fileName'];
$filePath = GX_PATH."/files/tmp/".$fileName;
$handle = fopen($filePath, "w");
fwrite ($handle, $file);
fclose($handle);
I hope somebody will help me to find what is wrong.
Thanks in advance.
EDIT:
I find another way which work on more browsers. I've discover FormData !
formData = new FormData();
formData.append("file", file);
SC.Request.postUrl(url).notify(this, 'formDataDidPost').send(formData);
This way, the PHP code is more simple :
$_FILES['file'];
I have done this using following method
$out = fopen($fileUploadPath ,"wb");
$in = fopen("php://input", "rb");
stream_copy_to_stream($in,$out);
This is just 3 methods I used to write the uploaded content to the server. Try it and see
Related
I'm using form data for uploading files code given below
var formData = new FormData();
/* Add the file */
formData.append("qqfile", file);
xhr.open("post", 'upload.php', true);
xhr.setRequestHeader("Content-Type", "application/octet-stream");
xhr.send(formData); /* Send to server */
in upload.php my code is given below
$fileReader = fopen('php://input', "r");
$fileWriter = fopen($this->_destination_file, "w+");
while(true) {
$buffer = fgets($fileReader, 4096);
if (strlen($buffer) == 0) {
fclose($fileReader);
fclose($fileWriter);
return true;
}
fwrite($fileWriter, $buffer);
}
return false;
when i'm trying to upload pdf file it's working perfectly, when I'm trying to upload .xls file,file is uploaded but when i open xls file getting non readable character.
I want to upload image file into alfresco using cmis api using PHP..
I can create simple text document in alfresco using following code
$obs = $client->createDocument($myfolder->id, $repo_new_file,$prop, "testssss", "text/plain");
I tried following code to upload image
$obs = $client->createDocument($myfolder->id, $repo_new_file,$prop, null, "image/jpeg");
But can't create image file into alfresco
Can anyone help me to solve this issue ??
I got the solution of this problem
Just store base64 content into image.. Use below code
$filename="A.jpg";
$handle = fopen($filename, "r");
if(!$handle)return FALSE;
$contents = fread($handle, filesize($filename));
if(!$mimetype)$type=mime_content_type($filename);
else $type=$mimetype;
fclose($handle);
$base64_content=base64_encode($contents);
$obs = $client->createDocument($myfolder->id, $repo_new_file,$prop, base64_decode($base64_content), "image/jpg");
I made a script where I download the images from a dropbox folder to my computer using PHP.
What I try to do now is to download thumbnail of the images instead of the whole image.
For this I use the: GetThumbNail method from the Dropbox API.
Here is part of the code:
// download the files
$f = fopen($img_name, "w+b");
$fileMetadata = $dbxClient->getThumbnail($path, 'jpeg','xl');
fclose($f);
When I run this the images I get are 0 size and they have no content. Any ideas what I am missing?
Thanks
D.
EDITED
$f = fopen($img_name, 'w+b');
$thumbnailData = $dbxClient->getThumbnail($path, 'jpeg', 'xl');
fwrite($f, $thumbnailData);
fclose($f);
You're opening and closing $f without ever writing anything into it.
getThumbnail returns an array with two elements: the metadata for the file and the thumbnail data.
So I think you'll want something like this:
$f = fopen($img_name, 'w+b');
list($fileMetadata, $thumbnailData) = $dbxClient->getThumbnail($path, 'jpeg', 'xl');
fwrite($f, $thumbnailData);
fclose($f);
I'm using Valum's file uploader to upload images with AJAX. This script submits the file to my server in a way that I don't fully understand, so it's probably best to explain by showing my server-side code:
$pathToFile = $path . $filename;
//Here I get a file not found error, because the file is not yet at this address
getimagesize($pathToFile);
$input = fopen('php://input', 'r');
$temp = tmpfile();
$realSize = stream_copy_to_stream($input, $temp);
//Here I get a string expected, resource given error
getimagesize($input);
fclose($input);
$target = fopen($pathToFile, 'w');
fseek($temp, 0, SEEK_SET);
//Here I get a file not found error, because the image is not at the $target yet
getimagesize($pathToFile);
stream_copy_to_stream($temp, $target);
fclose($target);
//Here it works, because the image is at the desired location so I'm able to access it with $pathToFile. However, the (potentially) malicious file is already in my server.
getimagesize($pathToFile);
The problem is that I want to perform some file validation here, using getimagesize(). getimagesize only supports a string, and I only have resources available, which result in the error: getimagesize expects a string, resource given.
It does work when I perform getimagesize($pathTofile) at the end of the script, but then the image is already uploaded and the damage could already have been done. Doing this and performing the check afterwards and then maybe deleting te file seems like bad practice to me.
The only thing thats in $_REQUEST is the filename, which i use for the var $pathToFile. $_FILES is empty.
How can I perform file validation on streams?
EDIT:
the solution is to first place the file in a temporary directory, and perform the validation on the temporary file before copying it to the destination directory.
// Store the file in tmp dir, to validate it before storing it in destination dir
$input = fopen('php://input', 'r');
$tmpPath = tempnam(sys_get_temp_dir(), 'upl'); // upl is 3-letter prefix for upload
$tmpStream = fopen($tmpPath, 'w'); // For writing it to tmp dir
stream_copy_to_stream($input, $tmpStream);
fclose($input);
fclose($tmpStream);
// Store the file in destination dir, after validation
$pathToFile = $path . $filename;
$destination = fopen($pathToFile, 'w');
$tmpStream = fopen($tmpPath, 'r'); // For reading it from tmp dir
stream_copy_to_stream($tmpStream, $destination);
fclose($destination);
fclose($tmpStream);
PHP 5.4 now supports getimagesizefromstring
See the docs:
http://php.net/manual/pt_BR/function.getimagesizefromstring.php
You could try:
$input = fopen('php://input', 'r');
$string = stream_get_contents($input);
fclose($input);
getimagesizefromstring($string);
Instead of using tmpfile() you could make use of tempnam() and sys_get_temp_dir() to create a temporary path.
Then use fopen() to get a handle to it, copy over the stream.
Then you've got a string and a handle for the operations you need to do.
//Copy PHP's input stream data into a temporary file
$inputStream = fopen('php://input', 'r');
$tempDir = sys_get_temp_dir();
$tempExtension = '.upload';
$tempFile = tempnam($tempDir, $tempExtension);
$tempStream = fopen($tempFile, "w");
$realSize = stream_copy_to_stream($inputStream, $tempStream);
fclose($tempStream);
getimagesize($tempFile);
I'm receiving files (images) uploaded with Ajax into my PHP script and have got it to work using this:
$input = fopen("php://input", "r");
file_put_contents('image.jpg', $input);
Obviously I will sanitize input before this operation.
One thing I wanted to check was the file size prior to creating the new file, as follows:
$input = fopen("php://input", "r");
$temp = tmpfile();
$realsize = stream_copy_to_stream($input, $temp);
if ($realsize === $_SERVER["CONTENT_LENGTH"]) {
file_put_contents('image.jpg', $temp);
}
And that doesn't work. The file is created, but it has a size of 0 bytes, so the content isn't being put into the file. I'm not awfully familiar with using streams, but I don't see why that shouldn't work, so I'm turning to you for help. Thanks in advance!
The solution was deceptively simple:
$input = fopen("php://input", "r");
file_put_contents($path, $input);
You are using file resources as if they were strings. Instead you could again use stream_copy_to_stream:
stream_copy_to_stream($temp, fopen('image.jpg', 'w'));