Save an Image to a file

L

Lee

Thanks Oliver!
I understand it a little better.

Yes I was thinking the same thing about $_FILES. In PHP, you need an
associative array with key-value pairs, and as far as I know, there is
nothing similar to the command line arguments. But in PERL, you have
command line stuff. Actually, Knute's idea to use perl might actually
be the better way to process the file when it comes in just because of
the stream handling.

I am going to test the code out with $_FILES anyway just in case I
might be able to get away with using PHP.
 
K

Knute Johnson

Lee said:
Thanks Oliver!
I understand it a little better.

Yes I was thinking the same thing about $_FILES. In PHP, you need an
associative array with key-value pairs, and as far as I know, there is
nothing similar to the command line arguments. But in PERL, you have
command line stuff. Actually, Knute's idea to use perl might actually
be the better way to process the file when it comes in just because of
the stream handling.

I am going to test the code out with $_FILES anyway just in case I
might be able to get away with using PHP.

Lee:

I wish I knew something about PHP to help you but I don't :). There
are some packages in Perl (that I didn't use here) to handle sending
files to cgi programs. I assume there must be similar things in PHP.

You know that this would be a trivial exercise to write the server in Java?
 
L

Lee

I have no clue about java, but I won't stop you if want to give me a
servlet or whatever it would be (JSP?).
I just found how to use command line arguments in PHP haha.
http://us3.php.net/manual/en/features.commandline.php

I wrote this PHP script as the receiving script and it gave me the
following output. For some reason, it is reading in the stream as a
blank input. But anyway, if you have a java script for me to use
somehow, I'd certainly try it. I just don't think I'm capable of
making it for myself.

======================
STDIN content



11:13:50
=====================

<?
// open the log file
$filename='log.txt';
$fp=fopen($filename,'w+');

// see which kind of input I'm getting
if($_FILES){
$str="files\n\n" . print_r($_FILES,true);
}
elseif($_POST){
$str="post\n\n" . print_r($_POST,true);
}
elseif($_GET){
$str="get\n\n" . print_r($_GET,true);
}
elseif(STDIN){
$max=1*10^6;
$stdin = fopen('php://stdin', 'r');
$content = fread($stdin,$max);
$str="STDIN content\n\n" . $content;
}
else{
$str="no files found";
}

// write to the log file
$str .= "\n\n" . date('G:i:s',time());
fwrite($fp,$str);
fclose($fp);

// display the log file
header("Content-type: text/plain");
include $filename;
?>
 
L

Lee

Nevermind, I just tested for the length of the argument, and it was 0.
If you have that java code, I'd try it out!
 
L

Lee

I'm going back to the string route. I made an easy-to-use class that
can successfully connect to my remote script. It uses the Scanner
class, which requires the newest version of Java.

import java.net.*;
import java.io.*;
import java.util.Scanner;

class PostRequest{
private String path;
private String request="&"; // the request string
private String outputPage=""; // the page that is returned

// getters
public String getPath(){
return path;
}
public String getRequest(){
return request;
}
public String getOutputPage(){
return outputPage;
}

// setters
public void setPath(String path){
this.path = path;
}

// add key/value pairs for the request
public void addRequest(String key, String value){
this.request += key + "=" + value + "&";
}

public void connect(){
try{
URL url = new URL(path);
HttpURLConnection con = (HttpURLConnection)url.openConnection();
con.setDoInput(true);
con.setDoOutput(true);
con.setUseCaches (false);
con.setRequestMethod("POST");

// write the parameters
OutputStreamWriter out = new
OutputStreamWriter(con.getOutputStream());
// Send data to host
String data = this.getRequest();
out.write(data, 0, data.length());
out.close();

// read the file that was written to
InputStream is = con.getInputStream();
Scanner pageScanner = new Scanner(is);
while (pageScanner.hasNext()){
outputPage += pageScanner.nextLine() + "\n";
}

is.close();
con.disconnect();
}
catch(IOException e){
System.out.println("error: " + e.getMessage() + "\n\n" + e + "\n" +
path);
}
}

// constructor
public PostRequest(){

}

public static void main (String[] args){
PostRequest postRequest = new PostRequest();

String path = "http://example.com/something.php";

postRequest.setPath(path);
postRequest.addRequest("php","cool");
postRequest.addRequest("Java","difficult");

postRequest.connect();

System.out.println (postRequest.getOutputPage());
}
}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,432
Messages
2,571,682
Members
48,796
Latest member
Greg L.

Latest Threads

Top