Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations derfloh on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Open a file command fails 1

Status
Not open for further replies.

CherylD

Programmer
Joined
May 1, 2001
Messages
107
Location
CA
I've tried everything, and even though I know that there are FAQ's but I still can't get this to work. Here is the code:
#UPLOAD THE FILE/PICTURE
my $outfile ="/the/path/on/my/webserver/pics/$tempID.jpg";
my $data = "";

open(IN, &quot;<$file&quot;) || die &quot;Could not read $file, $!&quot;;
open(OUT,&quot;>$outfile&quot;) || die &quot;Could not read $outfile, $!&quot;;

binmode IN;
binmode OUT;
while (read(IN, $data, 16384)) {
print OUT $data;
}
close OUT;
close IN;

And the error log says:
Could not read C:\Documents and Settings\cheryl\My Documents\My Pictures\goofypic.jpg, No such file or directory at /the/path/on/mywebserver/cgi-bin/subdir/script.cgi line 337.
Database handle destroyed without explicit disconnect.

The script and location for the files to be upload to are on my unix server. I am running the script by connecting via a browser from my Windows PC. How do I get it to open the file from my local machine/or whoever's machine running it from the browser without the script wanting to upload that file from itself?

Please help!!!!

Thanks,
Cheryl
 
Unless you have a server running on your local PC that you can connect to from the script (using LWP or something like that), you can't. How are you getting the upload file name? If you're using a <input type=&quot;file&quot;> the browser should automatically upload the file to a temporary file on your server, and the value of that control should be the name of the temporary file it created. Then you just copy that file to where you want it.
Tracy Dryden
tracy@bydisn.com

Meddle not in the affairs of dragons,
For you are crunchy, and good with mustard.
 
Cheryl, I assume that, as Tracy mentioned, you are probably doing

<input type=&quot;file&quot; name=&quot;file&quot;>

in your form to get the name of the file that is being uploaded into variable $file.

I coded an upload, quite a long time ago, that looks very similar to yours - I didn't use the &quot;binmode&quot; on IN or OUT, but there is one other very important difference between yours and mine - I could *NOT* get the &quot;while(read&quot; loop to work using a *filehandle* for the input file - the only way it would work for me is to name $file right in the read - instead of naming the filehandle IN.

Here's what my upload HTML looks like:
-----------------------------
<input type=&quot;file&quot; size=&quot;40&quot; name=&quot;upload_file&quot;>

Here's what my &quot;while(read&quot; loop looks like:
-------------------------------

$total_size = 0;
### while ($size = read(IN_FILE,$data,16384)) { ### doesn't work ###
while ($size = read($upload_file,$data,16384)) {
print NEW_FILE $data;
$total_size += $size;
}
close NEW_FILE;

Notice that where you have &quot;IN&quot;, I have &quot;$upload_file&quot;.

HTH.
Hardy Merrill
Mission Critical Linux, Inc.
 
hmerrill thank you!!! It's not almost working!!!!!

One problem still ..... the file must get corrupted or something because although when I look at them on the server they say that they are there and they have a file size now (woohoo!!) which is correct, but when I view them I don't see any picture just a lil' red x in them....any one have any ideas?

 
I assume you meant NOW almost working? :-)
Usually the red 'x' isn't caused by a corrupted file, but by a &quot;file not found&quot;. Check to make sure your links to the files are correct.
Tracy Dryden
tracy@bydisn.com

Meddle not in the affairs of dragons,
For you are crunchy, and good with mustard.
 
Yes, I did mean NOW! Ooops... As for the image problem, I am actually using an ftp program and downloading it locally to my pc to view and that's when I can't see it.

As for viewing it via html <img....> I thought that I would get the same thing, but apparently I don't. I just tested that and it works!!!!!!

Obviously it's a monday today.

Thanks everyone for your help.
 
First: Search cgi-lib.pl (free download at
2. Save HTML file with:
<HTML>
...........

<FORM ACTION=&quot;img_upload.cgi&quot; enctype='multipart/form-data' METHOD=&quot;POST&quot;>
<INPUT TYPE=FILE NAME=&quot;imgURL&quot;>
................
</FORM>
...........
</HTML>

3. Save Perl img_upload.cgi with

#!/usr/bin/perl

require &quot;cgi-lib.pl&quot;;

# the input fields/verbose names:
%input = (
'imgURL', 'image file'
);

%sfn = ();
%cfn = ();
#read the form input##############
&ReadParse(\%f,\%cfn,\$ct,\%sfn);
##################################

$fpath = $cfn{'imgURL'};
if(! $cfn{'imgURL'}) {
#print error message for requiring option
};
if($fpath =~ m/[\/\\]([^\/\\]+)$/) {
$fname = $1;
} else {
$fname = $fpath;
};

#check images type if neccesary!

$main_dir =$your_destination_directory;
rename($sfn{'imgURL'},&quot;$main_dir/$fname&quot;);
chmod 0666,&quot;$main_dir/$fname&quot;;
$f{'imgURL'} = $fname;

#print result messages here

Note: Please edit cgi-lib.pl file and change the tempolary directory where your server store in buffering.

Enjoy,
htruong
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top