I have a weird problem. I want to upload some data with WinInet to a PHP script.
When I upload the data at once with HttpSendRequest(), then PHP reads the uploaded data correctly, e.g.
$entityBody = file_get_contents('php://input');
When I upload the data in parts with HttpSendRequestEx() and InternetWriteFile() , then the same data is uploaded, but PHP fails to read the input (empty).
What could be wrong?
Is the PHP script "called" before the entire data is uploaded?
If so, how do I get the data?
Found it, Content-Length header was missing.
Related
I've spent good 2 days on this issue and I'm reaching a desperation point. I am hitting my request limits when I am uploading file content via Ajax - Request Entity Too large. I am on an Apache server. I am able to modify php.ini and htaccess but not able to modify any configs beyond this as I do not have access to those.
Problem:
I have a base64 encoded content of an image that I need to upload to my server (roughly 1MB in size). I have tried uploading this value, which results in 413 error code. As an experiment, I've tried uploading an image using a <input type='file' /> field which works for images and files much bigger than 1MB. It seems I am hitting the 413 error only if I have data >1MB present in the request params (eg adding data in an input field, or adding base64 representation of an image into one of the request params). Uploading the same file using File input works fine though.
The real issue is that in my website I can't have users upload image via File Input field. I only have the base64 content of the image that needs to be uploaded. Given that I can't modify my server settings, is there a way I can upload this content into my server??
Additional Info:
I am using JS FormData() object. The content of my form goes into this object and gets uploaded to my server. I can dynamically add values to this object using methods like formdata.append().
If I append large base64 string from above description, I hit a 413 error.
If I add a text field into the form and copy/paste the base64 string into the input field, I also hit the 413 error
If I upload the same image from my machine using <input type='file' /> field and upload the FormData object the image comes through fine.
Hope this is enough info. As a bonus - if anyone can explain why uploading the file via File Input works, but uploading the same data via a text field doesn't that would be quite beneficial also!
For anyone having similar problems in the future, I've found the answer in one of the answers to this question
Essentially we'll add the base64 string as a blob to the formData object using:
var formData = new FormData();
var blob = new Blob(['Lorem ipsum'], { type: 'plain/text' });
formData.append('file', blob,'readme.txt');
This seems to mimic the behaviour of File Input.
I'm trying to upload a file to a php script running on server via a python script:
PYTHON CODE:
file = open(new_alert.alarmpdfurl, 'rb').read()
r = requests.post('https://www.firerun.at/dbadmin.php?'+data,files={'file':('alarmpdf.pdf', file)})
The problem is I can not find the file in the $_POST or $_SERVER variables.
The question is how do I get the file in the PHP script to upload it to my MySQL DB as BLOB.
$_FILES
http://php.net/manual/en/reserved.variables.files.php
Similar to $_POST, but specific to uploaded files.
Well, you need to read the contents of the uploaded file first, using a function like file_get_contents("absolute-path-to-uploaded-file").
The value of "absolute-path-to-uploaded-file" should be in the $_FILES array.
The value returned by file_get_contents function can be saved to the blob column in MySQL database
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.
I know it is possible to read file content from $_FILES["file"]["tmp_name"] and save the content into DB.
But I think it is a performance waste.
Is it possible to read the file content directly from the HTTP post stream?
No, PHP has an internal data structure (the $_FILES array) that stores all uploaded file info, and there is no other way of accessing that info elsewhere in PHP.
Its possible via PUT ..... you can get the raw input ... but you would still need to parse it your self
$input = file_get_contents('php://input');
I think $_FILES is faster and better
This is not possible with POST request.
With PUT requests, you can use stdin.
$putdata = fopen("php://input", "r");
I've got a script, largely based on an example uploading PHP file from jQuery Uploader. It gets file type with the following code (it gets this $_FILES component)...
$fileType = (isset($_SERVER['HTTP_X_FILE_TYPE']) ? $_SERVER['HTTP_X_FILE_TYPE'] : $upload['type']);
Note; $upload['type'] comes from the $_FILES['files']['type'].
Now, this is fine - except for the fact that some files seem to have no fileType information from this. I can get more accurate responses from using file info and mimetype functions in PHP - but they don't work on $_FILES objects and I'm trying to do this check before I transfer the file to s3 so I don't really want to load it locally.
Can anyone advise if there's something I can to get more accurately report type from $_FILES or is it going to have to load locally in order to run these alternative PHP functions?
finfo is the only way to do this. You cannot rely on information the client sends you, it is far too easy to fake from the client side.
There is no reason that it won't work with $_FILES, you would simply pass $_FILES['files']['tmp_name'] as the file path - this is still a valid file path, and you don't need to call move_uploaded_file() to access the data. Leaving the file in the temp location also has the advantage that it will be destroyed when the script is finished if you haven't done anything with it.