Post a file to a .php page from Oracle - php

On my website I have a .php script to which our customers can post orders.
$destname = CreateUniqueOrderFileName();
if (is_uploaded_file($_FILES['file']['tmp_name'])) {
if (move_uploaded_file($_FILES['file']['tmp_name'], $destname))
echo "OK";
else
echo "ERROR: move";
}
So for my clients I wrote an app in C# and if I upload an order I do something like this:
var client = new WebClient();
byte[] response = client.UploadFile("http://mywebsite/order.php", "POST", orderFile);
But now I have a customer who uses Oracle and wants to post an order using PL/SQL.
I try to tell him he has to do a file uploaded via HTTP POST and use multipart/form-data but he does not understand me. Maybe I'm using the wrong vocabulary or not using standards.
So does someone have an example of how to post a file to a .php script in PL/SQL or a suggestion where to find more information uploading a file to a .php webpage with an Oracle client.

I think you can use PL/SQL package UTL_HTTP to do a HTTP POST
http://awads.net/wp/2005/11/30/http-post-from-inside-oracle/
Another option might be to use a Java Stored Procedure

We could find a way to do a POST request using the Content-Type multipart/form-data according to RFC2388. So I changed the .php script to something like this.
$fp = fopen('php://input','r');
$content = stream_get_contents($fp);
$destname = CreateUniqueOrderFileName();
$isWritten = #file_put_contents($destname, $content);
if($isWritten===false)
{
echo "ERROR: could not write file: ".$destname;
return;
}
Using this we where able to post the order using the UTL_HTTP lib to do a HTTP POST.

Related

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.

php: not getting file what does header() do?

I am very new to PHP. I am trying to make simple script which allows the users to download files from a webpage.
<?php
require_once('Connections/connection_psfriend.php'); ?>
$receivedfilerequest = addslashes($_REQUEST['filesource']);
$file_path = $_SERVER['DOCUMENT_ROOT'].'/'.'ps-friend'.'/' . $receivedfilerequest;
if(file_exists( $file_path)){
echo 'The file exists';
$size = filesize($file_path);
echo $size; //Its working perfectly fine till here.
exit;
}
But what do I do next so that this file is actually downloaded. I saw some tutorials and they use header("Location: ".$file_path); after this. But that's not working for me. What does this header function actually do, and what should be the code to actually download this file?
You can use the PHP's readfile (http://php.net/manual/en/function.readfile.php) to force a file download, if that's what you are trying to do. There is an example on that page shows you how to download an image.
The PHP header function sends a raw HTTP header to the client. The location header is usually used to redirect the client to a new page in PHP.
please refer this URL, it is used to download file
http://php.net/manual/en/function.readfile.php

upload file to a server using lua

I have been trying to upload a file to a webserver using LUA.
My problem is that I want to upload a file using LUA to webserver which mimics uploading a file like from a browser.
I was successfully able to upload file using server, where I can do file_get_contents('php://input'), where i get contents and mime_decode on it and save that file.
But, i want to achieve where i can do like $_FILES['file_name'], on server side using lua.
so does anybody have idea how to do this in LUA?
Regards.
You can use the HTTP sockets library for sending HTTP requests and ltn12 library for filters (file input). Both should be provided with Lua.
http = require("socket.http")
ltn12 = require("ltn12")
http.request{
url = "url://to.server/upload/script.php",
method = "POST",
headers = {
["Content-Type"] = "multipart/form-data",
["Content-Length"] = sizeOfFile
},
source = ltn12.source.file(io.open(pathToLocalFile)),
sink = ltn12.sink.table(response_body)
}
print(response_body[1]) --response to request

Offer a generated file for download from jQuery post

I've got a large form where the user is allowed to input many different fields, and when they're done I need to send the contents of the form to the server, process it, and then spit out a .txt file containing the results of the processing for them to download. Now, I'm all set except for the download part. Setting the headers on the response to the jQuery .post() doesn't seem to work. Is there any other way than doing some sort of iframe trick to make this work (a la JavaScript/jQuery to download file via POST with JSON data)?
Again, I'm sending data to the server, processing it, and then would like to just echo out the result with headers to prompt a download dialog. I don't want to write the result to disk, offer that for download, and then delete the file from the server.
Don't use AJAX. There is no cross-browser way to force the browser to show a save-as dialog in JavaScript for some arbitrary blob of data received from the server via AJAX. If you want the browser to interpret the results of a HTTP POST request (in this case, offering a download dialog) then don't issue the request via AJAX.
If you need to perform some kind of validation via AJAX, you'll have to do a two step process where your validation occurs via AJAX, and then the download is started by redirecting the browser to the URL where the .txt file can be found.
Found this thread while struggling with similar issue. Here's the workaround I ended up using:
$.post('genFile.php', {data : data}, function(url) {
$("body").append("<iframe src='download.php?url="+url+"' style='display: none;'></iframe>");
});
genFile.php creates the file in staging location using a randomly generated string for filename.
download.php reads the generated file, sets the MIME type and disposition (allowing to prompt using a predefined name instead of the random string in the actual filename), returns the file content and cleans up by deleting the source file.
[edit] might as well share the PHP code...
download.php:
<?php
$fname = "/tmp/".$_GET['url'];
header('Content-Type: text/xml');
header('Content-Disposition: attachment; filename="plan.xml"');
echo file_get_contents($fname);
unlink ($fname);
?>
genFile.php:
<?php
$length = 12;
$chars = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
$str = substr( str_shuffle( $chars ), 0, $length ).'.xml';
$fh = fopen(('tmp/'.$str), 'w') or die("can't open file");
fwrite($fh,$_POST["data"]);
fclose($fh);
echo $str;
?>
Rather than using jQuery's .post(), you should just do a normal POST by submitting the form, and have the server respond with appropriate Content-Encoding and MIME-type headers. You can't trigger a download through post() because jQuery encapsulates the returned data.
One thing I see in use rather frequently, though, is this:
$.post('generateFile.php', function(data) {
// generateFile builds data and stores it in a
// temporary location on the server, and returns
// the URL to the requester.
// For example, http://mysite.com/getFile.php?id=12345
// Open a new window to the returned URL which
// should prompt a download, assuming the server
// is sending the correct headers:
window.open(data);
});

Actionscript - AS 2.0 - PHP copy file from server to server THEN run function

Basic Idea: I have a flash file that takes screenshots with a click of a button, sending the data to a PHP file, and then the user gets to save a PNG image. The images that are merged together (via PHP) require that they reside on the same server as the PHP, otherwise they do not merge and the final PNG shows up blank.
My solution so far: I have two PHP files, and I just need to find a way to merge them. The screenshot one, and one that copies a file from one server to another. This is my cheat work around to bring the image to reside on the same server, THEN run the screenshot php.
The Server-to-Server PHP Code:
<?PHP
$inputfile = FOPEN("https://www.google.com/intl/en_com/images/srpr/logo3w.png", "r");
$outputfile = FOPEN("transferedfile.gif", "w");
ECHO "File opened...";
$data = '';
WHILE (!FEOF($inputfile)) {
$data .= FREAD($inputfile, 8192);
}
ECHO "Data read...";
FWRITE($outputfile, $data);
ECHO "transfered data";
FCLOSE ($inputfile);
FCLOSE ($outputfile);
ECHO "Done.";
?>
So as you can see, it pulls Google's logo and saves it as "transferedfile.gif" to the directory the PHP resides on. I can get this PHP code to work by saving this as whateverIWant.php on my webserver, and visiting it directly, but I need to in place of Google's logo (in this example) put a value that will be dynamically changing via flash.
So basically… in the flash file, I'll have a dyniamic variable where the URL will change, in short. So we'll just say that I define that variable in flash as var imageToGet so somehow I need to pass that variable into this PHP. That's one step... here's the AS 2.0 code:
My Actionscript (2.0) Code:
button.onRelease = function ():Void {
sendImageToServer();
ScreenShot.save(_root, "screenshot.png", 0, 0, 100, 140);
};
the sendImageToServer() function isn't made yet. This is where I'm stuck. I would need the sendImageToServer() function to send var imageToGet as what image to get, THEN run the ScreenShot.save() function after the transfer is done (aka FCLOSE ($outputfile); is complete)
In Summary: A movie clip on the stage will have a dynamic image loaded into it, that once a button is pressed, it would need to copy that dynamic image to the local server, and then run the screenShot function. I believe once I have this figured out, I should be able to do everything else, such as saving as a unique name, saving multiple files, etc. But I just need pushed in the right direction :)
Thanks so much everyone # StackOverflow. You've been nothing but awesome to me thus far!
EDIT -- I've found a good starting point!!
I found a good starting point, and am answering my own question in case someone else stumbles upon this. I used these two codes as a starting point, and I think I'm on the right track…
In Flash: I simply made a dynamic textbox with the instance name of traceText
In Actionscript (2.0):
var send:LoadVars = new LoadVars;
var receive:LoadVars = new LoadVars;
send.toPHP = "asd123";
receive.onLoad = function(){
encrypted = this.toFlash;
traceText.text = encrypted;
}
send.sendAndLoad("test.php",receive,"POST");
In "test.php" file:
$fromFlash = $_POST['toPHP'];
$encrypted = $fromFlash;
$toFlash = "&toFlash=";
$toFlash .= $encrypted;
echo $toFlash;
What this ended up doing was sending the variable to PHP and then back again. Which is perfect for what I needed. For now, I should be good! Hope this helps anyone that needs it.

Categories