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 strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Problem with Picture Upload

Status
Not open for further replies.

Mttkdred

Programmer
Jul 21, 2010
5
US
Hello,

I am having a problem with setting up a picture uploading feature. When I use the write to file script it creates the file, but the file ends up being blank. It uses the right file name I specify and goes to the right directory.

This is what my code looks like:

open ( UPLOADFILE, ">$upload_dir/$n_listing_5_picture_6" ) or die "$!";
binmode UPLOADFILE;

while ( <$upload_filehandle35> )
{
print UPLOADFILE;
}

close UPLOADFILE;





What could be wrong? I have 35 total picture uploads in my script (this is the 35th). $n_listing_5_picture_6 is the file name I specify in a variable.

I am not showing the whole script because the entire script is over 2000 lines long. Please help me!
 
The only possibility I see is that nothing is read from [tt]$upload_filehandle35[/tt] .
Are you setting [tt]binmode[/tt] also for this one? Otherwise an end of file char at the beginning would stop the reading.

Franco
: Online engineering calculations
: Magnetic brakes for fun rides
: Air bearing pads
 
That binmode applies to the output file. Is there another such command earlier in the script applied to $upload_filehandle35? If not, try adding this line before the while loop:

Code:
binmode $upload_filehandle35;

(In case you were wondering, file handles can be referred to directly, as is the case with UPLOADFILE or indirectly through a variable, as is the case with $upload_filehandle35.)

Annihilannic.
 
Thank you for the help everyone! I am still having issues though. I have an extremely long program of about 2200 lines before it gets to the HTML print area, so if anyone would be willing to help I am going to attach it below. It is really just the same exact procedure repeated 35 times however. I hopefully have labeled everything OK, so if you have time it is below...

THANK YOU EVERYONE FOR YOUR TIME AND HELP! :)
 
 http://classifiedsadvantage.com/Code.txt
I placed code tags around my script by-the-way to try to make it formatted well for this forum.

Just a side note...
 
Really a verboooose code!
There is no [tt]binmode[/tt] for the input files. You definitely should add one for each of the 35 input files in the way suggested by Annihilannic.
However, as you are using the upload function of CGI, the file could be corrupted during the upload phase (personally don't know much of CGI.pm).
This part of CGI's documentation seems relevant to your problem. It suggests ways you can use to debug your problem (of course you know that you are venturing in a field that's not for beginners...)
When a file is uploaded the browser usually sends along some information along with it in the format of headers. The information usually includes the MIME content type. Future browsers may send other information as well (such as modification date and size). To retrieve this information, call uploadInfo(). It returns a reference to an associative array containing all the document headers.

$filename = param('uploaded_file');
$type = uploadInfo($filename)->{'Content-Type'};
unless ($type eq 'text/html') {
die "HTML FILES ONLY!";
}
If you are using a machine that recognizes "text" and "binary" data modes, be sure to understand when and how to use them (see the Camel book). Otherwise you may find that binary files are corrupted during file uploads.

There are occasionally problems involving parsing the uploaded file. This usually happens when the user presses "Stop" before the upload is finished. In this case, CGI.pm will return undef for the name of the uploaded file and set cgi_error() to the string "400 Bad request (malformed multipart POST)". This error message is designed so that you can incorporate it into a status code to be sent to the browser. Example:

$file = upload('uploaded_file');
if (!$file && cgi_error) {
print header(-status=>cgi_error);
exit 0;
}
Another suggestion could be to place your 35th file in an upper position in the sequence of uploads: if the problem is in the file, you'll get the same result. Otherwise there could be some problem with a consumed buffer.

Franco
: Online engineering calculations
: Magnetic brakes for fun rides
: Air bearing pads
 
I feel like a toddler on a bike with training wheels. I am such a newbie. Again, THANK YOU EVERYONE! I have yet a another question... How would I rewrite this script with all the information above? (I am having problems understanding some of what was said)

open ( UPLOADFILE, ">$upload_dir/$n_main_picture_1" ) or die "$!";
binmode UPLOADFILE;

binmode $upload_filehandle1;

while ( <$upload_filehandle1> )
{
print UPLOADFILE;
}
 
That's correct.
If you get the same result, try taking that entire block for the file #35 (the one with problems) and place it 10 or 15 positions above, between, for example, the block for filehandle19 and filehandle20. If still you get a void file, then the uploaded file has a problem and you should inspect its binary content.
Otherwise report what happens.
However it will be difficult to follow you in debugging this problem, if you are not able to understand the results and follow other routes. And rewriting (and testing) the script is a big duty, and anyway wouldn't be of much use to you, as you can't understand it. You should look for a turnkey application doing what you intend to do.

Franco
: Online engineering calculations
: Magnetic brakes for fun rides
: Air bearing pads
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top