We closed this forum 18 June 2010. It has served us well since 2005 as the ALPHA forum did before it from 2002 to 2005. New discussions are ongoing at the new URL http://forum.processing.org. You'll need to sign up and get a new user account. We're sorry about that inconvenience, but we think it's better in the long run. The content on this forum will remain online.
IndexProgramming Questions & HelpOther Libraries › Help with SaveToWeb, save .txt to server
Pages: 1 2 
Help with SaveToWeb, save .txt to server (Read 4715 times)
Help with SaveToWeb, save .txt to server
Mar 20th, 2009, 10:51pm
 
Hello!  I'm trying to use the SaveToWeb script posted by Seltar, here:

http://processing.org/hacks/hacks:savetoweb

which uses a php file "saveFile.php," included below the Processing code.  I think I implemented it correctly, but always get a "Error #1 Uploading File."  To be precise:

Warning: copy(/test/test.txt) [<a href='function.copy'>function.copy</a>]: failed to open stream: No such file or directory in [the path to the php file] on line 21
and line 21 of the php file reads:
if (!copy($data, $newfile)) echo "Error #1 Uploading File."
Which seems to be a standard way of reporting copy failure in php.

All the permissions are set to 777 and the paths are right.  The author has gotten it to work, but I can't reach him.

A possibility: in the php file, I can't tell where the variable $data is being defined.  Could that be the cause?  I would be grateful for help with this script or a pointer to a new script.  The goal: read/write to text files on my server, and create new text files.

Thanks,
Ben


Re: Help with SaveToWeb, save .txt to server
Reply #1 - Mar 20th, 2009, 11:07pm
 
ben_hem wrote on Mar 20th, 2009, 10:51pm:
a pointer to a new script.
Perhaps see Re: save to web --- image export.
Alternative: if the text isn't long, just pass it in the URL, as parameter to the PHP script.
Re: Help with SaveToWeb, save .txt to server
Reply #2 - Mar 21st, 2009, 2:06am
 
Nice!  Thorough comments.  I am trying your example.  Unfortunately I get a

Fatal error: Call to undefined function:  scandir() in [path to Upload.php] on line 147.

Checked and it seems my service provider uses php-4, and scandir is php-5.  Sent them a note about that.  In the meantime, I replaced scandir() with this:

$dh  = opendir($destDir);
while (false !== ($filename = readdir($dh))) {
   $files[] = $filename;
}
sort($files);

which I found here http://us.php.net/scandir  .
But I got more errors than I could shake a stick at.  How can I work around this in php-4?

Thanks,
Ben
Re: Help with SaveToWeb, save .txt to server
Reply #3 - Mar 21st, 2009, 2:46am
 
Update:
I removed the whole max-files-checking block:

$files = scandir($destDir);  .... to ....  unlink($destDir . '/' . $file);}

and got the two following errors:

Warning: move_uploaded_file(...[my absolute url]/uploads/20090320-203107-snapshot.png) [function.move-uploaded-file]: failed to open stream: HTTP wrapper does not support writeable connections. in [path to Upload.php] on line 126
Warning:  move_uploaded_file() [function.move-uploaded-file]: Unable to move '/tmp/phpNN0rWX' to '...[my absolute url]/uploads/20090320-203107-snapshot.png' in [path to Upload.php] on line 126
Debug data: Uploaded file is in: /tmp/phpNN0rWX

So, progress!  New errors.  Where is this tmp directory created?  Do you know why this is happening?

Thanks,
Ben
Re: Help with SaveToWeb, save .txt to server
Reply #4 - Mar 21st, 2009, 10:07am
 
Oh, I thought that in 2009, all Web hosts, including those offering free service (like mine) have switched to PHP5!

Anyway, you seem to advance nicely.

"Where is this tmp directory created?"
Well, as shown, in /tmp... Smiley
Paths starting with slash are absolute in Unix, /tmp is the standard system temp dir, at the root of the filesystem. By design, it has all read/write access. BUT, it seems you have more restrictions from your host. Or you need to change some stuff.

The line
$destDir = '../Uploads';
defines where is the Uploads dir, relative to the location of the script.
The Uploads dir (you can rename it, of course) must exist, and you (your script!) must have read and write access to it. You should be able to do that with your FTP client.
Re: Help with SaveToWeb, save .txt to server
Reply #5 - Apr 2nd, 2009, 7:59am
 
Hopa!
Replaced $destDir in Upload.php with the absolute server path rather than the "http://..." url.  Works like a charm, I have many multicolored hearts!  Now if I can just tweak it to save .txt files and modify existing .txt's, I'm in business.

Thanks!
Ben

[edit:]
AHA.
turns out SaveToWeb's dirname($_SERVER["PATH_TRANSLATED"]); wasn't returning any data, so replacing it with the same path string as $destDir fixed the problems.  Tomorrow the world.

(are you thinking what I'm thinking, Pinky?
Gee, I think so, Brain.  But me and Pippy Longstocking?  What would the children look like?)
Re: Help with SaveToWeb, save .txt to server
Reply #6 - Jan 3rd, 2010, 11:14pm
 
BenHem, I've been going quite crazy trying to get these examples working with the php code. While I've gotten all the permissions set and tested a regular image uploading php script, I can't quite seem to get all the pieces together to make a processing sketch that saves an image on the server.

Would you still have some code around to help me out with this? I can't tell from your conversation here which code you ended up using, and making the adjustments to. I'd really appreciate a little more "spelling it all out" from someone. Thanks, _mike
Re: Help with SaveToWeb, save .txt to server
Reply #7 - Jan 4th, 2010, 6:30am
 
As shown above, so many things can go wrong... Depends on your server configuration (Unix? Windows?), PHP version, and so on.

So reporting some error messages you get might help in finding out what goes bad...
Re: Help with SaveToWeb, save .txt to server
Reply #8 - Jan 4th, 2010, 10:06am
 
Ok, great. Thanks so much for the reply.
I can see that I'm using PHP Version 5.2.6 on the server, and the server software is Apache/2.0.54. I'm clueless enough to not even know how to get error messages outside of navigating to the PHP page directly.

I did get a simple standard image uploading PHP script and input form installed and successfully uploading files to a folder with the permissions set to 777. But, I'm not sure what to alter in the sketch or the php to get this part working. I have tried the PostToWeb library and a number of different example sketches including your 3 files from another thread,
posted on pastebin -- Upload.php, ImageUpload.pde, DataUpload.java

In there I can see a few paths to change, the '../Uploads' and '/Tests/Uploads/' path in Upload.php, and "h t t p : / / w w w  . example.com/Tests/Processing" (sorry, not allowed to post with web links yet) in the DataUpload.java file. I'm not sure if there's a problem with absolute or relative paths in my attempts or something else entirely.

If you could explain how to get some error reports maybe I can figure this out. If there's another example that you think I should test out please let me know.
Re: Help with SaveToWeb, save .txt to server
Reply #9 - Jan 5th, 2010, 5:54am
 
Well, when you have a PHP error, it is reported on the browser in the returned page, so it should be quite obvious.
You can also trace things with echo, which adds the strings into the outputted page (must be HTML, use <br> to have lines, etc.).
Re: Help with SaveToWeb, save .txt to server
Reply #10 - Jan 5th, 2010, 10:34pm
 
thanks, I was able to get this uploading from my sketch running locally to my server. I did finally locate the errors being reported in the IDE, so headslap that I hadn't been looking there.

Some of the error messages about scandir() like "failed to open stream: no suitable wrapper could be found" lead me to a page that suggested that I add the following lines to my php.ini file:
"allow_url_fopen = On"
"allow_url_include = On"

I also was able to identify a path problem in the Upload.php file which was the last piece of this puzzle.

Wow, and now it's running on my web server and saving images too! Thank you sooo much for the help.
In the meantime I had been experimenting with Daniel Shiffman's http_post example, which I could get to save images on my server, but only by self-signing the applet (see processing hacks for more info on that).
Re: Help with SaveToWeb, save .txt to server
Reply #11 - Jan 6th, 2010, 2:24am
 
mshaub wrote on Jan 5th, 2010, 10:34pm:
Thank you sooo much for the help.

Obviously, you mostly self-helped yourself (not sure if that's proper English... Smiley).
Glad you sorted out things and shared the tips, I wouldn't have thought of these settings.
Re: Help with SaveToWeb, save .txt to server
Reply #12 - Jan 18th, 2010, 12:55pm
 
I have also been trying to save a txt file from an applet in a browser.  It seems BenHem was able to get this working.  If you have the time I (and most likely many others) would greatly appreciate a short explanation of how you did this.  

Thanks!

(I keep getting "Error # 2 uploading file.")
Re: Help with SaveToWeb, save .txt to server
Reply #13 - Feb 15th, 2010, 8:08am
 
After literally ages of failing and debugging I managed to get it working. This thread helped me to narrow down the problem so I want to post my fix, so it maybe helps others.

In the php (I called it upload.php), I had to change:
dirname($_SERVER["PATH_TRANSLATED"]); to
dirname($_SERVER['SCRIPT_FILENAME']);

and

if (is_uploaded_file($data)) to
if (is_uploaded_file($_FILES['data']['tmp_name']))

as well as

if (!copy($data, $newfile)) to
if (!copy($_FILES['data']['tmp_name'], $newfile))

and that is pretty much it.

On the processing side I had to change the filename obviously.

Works like a charm!
Re: Help with SaveToWeb, save .txt to server
Reply #14 - Feb 15th, 2010, 8:34am
 
I totally rewrote the PHP side of the hack because it was... well, just non-working. I also added lot of security stuff to avoid abuse of the script.
Pages: 1 2