receive image with PHP sent from FLASH - php

How can I save image with PHP which was uploaded with http post using FLASH?
To upload to i'm PHP using this:
var upload_to:*=new flash.net.URLRequest("url");
fileHandler.upload(upload_to);
And when I print $_FILES in PHP I get:
{"Filedata":{"name":"IMG_8​658 copy44.jpg","type":"applic​ation\/octet- stream","tmp_​name":"C:\\WINDOWS\\Temp\\​php35.tmp","error":0,"size​":183174}}
so the question is, how to form a file from that $_FILES variable?: ) Thanks

PHP doesn't store the file in memory. It's written out to a temporary file, which you can retrieve the name/path of from the tmp_name value (C:\WINDOWS...). The name field is the filename as provided by the client (IMG_8658...);
In your case, that'd be
$_FILES['Filedata']['tmp_name'] <-- location of temporary file
$_FILES['Filedata']['name'] <---original filename
$_FILES['Filedata']['size'] <--- size in bytes
$_FILES['Filedata']['type'] <-- mime type, as provided by the uploader
$_FILES['Filedata']['error'] <--- error code of upload operation (0 = a-ok)

use copy($_FILES['Filedata']['tmp_name'],'your destination path'); function.

Related

Can PHP read the meta files?

I built a parsial uploading in PHP and Client-Side Javascript.
The Flow
Convert the file to base64 so I get the base64 string
Slice the base64 string into some pieces
Post every piece and the position to upload handler
The upload handler make the post data as a single file in one same folder
If the iteration stop, the JS tell the upload handler to build the code
Then the upload handler make all files as a single file then decode the base64 to a new file
The problem
When the build finished, I can't read the result file as same as the $_FILES in file uploading. So I can't get:
Original size
Original extension
I wonder can PHP read the information about the file?
I expect some function like this (not the actual function)
$file = read_the_file('/path/to/file');
$metas = read_meta_file($file);
and then you can get what type of file and some others data.
Note
Currently, the file that I work with is a video file.
You can get the information about the file with the help of php function named pathinfo
How to use pathinfo:
print_r(pathinfo("/path_of_file/123.txt"));
It will return .....
Array ( [dirname] => /path_of_file [basename] => 123.txt [extension] => txt )
2. You can get the size of the file with the help of php function named filesize
How to use filesize:
echo filesize("123.txt");
It will return the size of file in bytes.
I hope, This will help you.
Good Luck.
You can use pathinfo php function
For file size you can use filesize function
To get extension from base_64 then you can use finfo_buffer function
echo finfo_buffer(finfo_open(), $base64, FILEINFO_MIME_TYPE);
But you would need to store somewhere that extension - in video files for example in ID3 tags (and use the lib https://github.com/JamesHeinrich/getID3/ to extract it). That is most likely NOT what you want to do as you would need to edit the file on the frontend.
Another option is to use mime_content_type(after you file has finished uploading) - for a "mp4" file, even without extension, it will give you "video/mp4", for mkv "video/x-matroska". Etc. based on the mime-type you can build the extension again - for example:
$exts = [
"video/x-matroska" => "mkv",
"video/mp4" => "mp4"
];
and then:
$extension = $exts[mime_content_type("..path..to..file")];
(of course first with checking if the entry exists there).
Extension itself is NOT stored in any metadata so you cannot simply extract it.
While having the mime-type you can also use any popular libs - like:
https://github.com/ralouphie/mimey
to convert mime-type to extension.
To get the file size it is best to simply use the filesize function.
Another option would be to simply send the original file name with the last post (the one which tells the backend that the file has finished): probably the most reliable one.

Is it possible to upload file without input type="file"

I want to use a text-box, insert my file address is in the Desktop, and then upload it in the server, what should I do?
Is it because you dont like file uploader object how it looks?
If yes I suggest you to make it hidden but in background use its fuctionality.
Show file selected information in a textbox but keep the file into hidden file uploder object.
Another method to post files like audio and jpeg to server is to use Base64 format . Its consider as string but not recommanded for large files.
Then you need a backend function to save data into file object or simply insert data into database.
Do it this way:
$file_url= $_POST['file']; //Path of file to be uploaded
if file_exists($file_url):
$fh= fopen("NewfileName", a+);
fwrite($fh, file_get_contents($file_url));
fclose($fh);
endif;

Unknown argument on a function on github

I found a good function on github for uploading images using php, but I do not know one of its arguments.
upload_image($_FILES,'file',250,'city',500,'../../uploaded/',1048576);
Function on Github
What is 'file' in this function?
Is this a trusted function to use in my website?
Considering the lines:
$file[$fileIndex]['tmp_name']
$file[$fileIndex]['error']
$file[$fileIndex]['name']
$file[$fileIndex]['type']
$file[$fileIndex]['size']
$file is a three-dimensional array, composed of arrays of name, tmp_name, type, size, error.
It is the kind of array you see when uploading files in PHP.
It calls move-uploaded-file, which moves an uploaded file to a new location.
This function checks to ensure that the file designated by filename is a valid upload file (meaning that it was uploaded via PHP's HTTP POST upload mechanism).
If the file is valid, it will be moved to the filename given by destination.

How can i get the content of uploaded file in php?

I can upload a file by using html file type and then I store that file information to mysql db. Here's my code=>
$upload = wp_upload_bits($_FILES["upload_file"]["name"], null, file_get_contents($_FILES["upload_file"]["tmp_name"]));
$document_name = $_FILES['upload_file']['name'];
$document_link = $upload['url'];
//and DB Operations in here..(I store to db filename,date,filelink etc.)
My problem is, I can't read the file content to store it to db. (I will do search on the file content, so I must read content of file.) Briefly how can i read the content of a file like pdf, doc or etc. from url such as http://...../uploads/exampleFile.docx?
$fileContent = file_get_contents($_FILES['upload_file']['tmp_name']);
Refer to the manual: $_FILES for an overview and this tutorial: Tizag PHP - File Upload for a walkthrough.
This PHP Manual section is a must-read as well: Handling File Uploads - moved from hakre's comment.

is it possible to change the name of a file after it's been uploaded

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);

Categories