Python upload PDF into MySQL database via PHP web service - php

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

Related

PHP: save a file (generated by aspx) to server

I have an url provided by a wholesaler. Url generates xml file which I need to save on my server.
I use PHP - file_get_contents and file_put_contents to do that:
$savepath = "path_to_my_server_folder";
$xmlurl = "http://usistema.eurodigital.lt/newxml/xmlfile.aspx?xml=labas&code=052048048048051057049050048049052";
file_put_contents($savepath.'eurodigital.xml', file_get_contents($xmlurl));
File is generated on my server, but its content is empty. I have no problems with other xml files if I provide direct xml url, but in this situation file is generated by aspx file dynamically. Xml url is actual url I use. When I open xmlurl in browser, xml file gets saved to device.
Can you help me with moving xml to my server? What function should I use? Is it even possible to do that using PHP 5? "allow_url_fopen" is ON.
Thank you in advance!

read contents of zipped files through file_get_contents in codeigniter in .srt format and upload to server

I am trying to read contents of zipped file as
$subda=file_get_contents('http://www.yifysubtitles.com/subtitle/thewilbyconspiracy1975dvdripxvid-english-128250.zip');
And trying to upload at my online server as below
$this->load->library('zip');
$data = $subda;
$name = 'myfile.srt';
$this->zip->add_data($name, $data);
$this->zip->archive('assets/subtitles/myzipfile.zip');
But when I check this uploaded file at my server it does not compressed properly.
it does not contain any data.
when I echo $subda it give results like.
Where I am wrong...
through file_get_contents I am already getting contents of zip file.
You cannot do:
$subda=file_get_contents('http://www.yifysubtitles.com/subtitle/thewilbyconspiracy1975dvdripxvid-english-128250.zip');
This will just load the ZIP content into the string rather than uncompressed.
See this on how you can read using a lib in php:
Best way to read zip file in PHP

WinINET and PHP php://input POST read

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.

PHP: Is it possible to save the uploading file content directly into DB without save to tmp directory

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

Getting mime type from uploading files - inconsistent

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.

Categories