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

Uploading jpgs 1

Status
Not open for further replies.

audiopro

Programmer
Apr 1, 2004
3,165
0
0
GB
I use this fairly standard upload routine for visitors to get images onto the server.
Code:
while($bytesread=read($fileID,$buffer,1024)){
	print OPF "$buffer";
	$sum_bytes=$sum_bytes+1024;
	if($sum_bytes >$MaxPicSize){
		close OPF;
		# Delete u/s picture
			unlink $NewFile;
		$Message .= "File is too large.<br>Maximum Allowed File Size = $MaxPicSize.<br>";
		last;
	}
}

The problem I have is 1 particular user is unable to upload images. She appears to be doing everything correctly although I can only go off her description of what she does.
Could there be something local causing the problem or is it likely to forever remain a mystery.

Keith
 
Hi

[ul]
[li]What means "is unable" in this context ?[/li]
[li]Are there relevant information in the web server logs ?[/li]
[li]Which browser/version she use ? Does that happens with other browsers/versions too ?[/li]
[li]Which operating system she use ? Does that happen with other operating systems too ?[/li]
[li]Are security software installed on her machines ? If she accesses internet through corporate proxy, are security software on that one ?[/li]
[/ul]


Feherke.
 
I do not have access to the server logs, she uses Windows Vista but not sure what version. It could be a security software issue, I will have to check what she has running.
It has to be something unique to her machine as there are hundreds of people uploading pictures, on a wide variety of setups, and only this one user reports problems.

The only issue I have found which affects upload is when someone trys to upload from another machine on a local network.



Keith
 
Keith what's the 'last' command in that if statement?

"In complete darkness we are all the same, only our knowledge and wisdom separates us, don't let your eyes deceive you."

"If a shortcut was meant to be easy, it wouldn't be a shortcut, it would be the way!"

MIME::Lite TLS Email Encryption - Perl v0.02 beta
 
OIC so a 'last' command exits a loop even if the loop control variable value is such that the loop would normally continue!

Is that any loop or just a while?

One other thing I notice you do that I don't is read the file in 1K chunks, is this important and what benefit do you get vs
Code:
    open(FILE, '<', "$file") or redirect();
    print <FILE>; 
    close(FILE);

Thanks,
1DMF

"In complete darkness we are all the same, only our knowledge and wisdom separates us, don't let your eyes deceive you."

"If a shortcut was meant to be easy, it wouldn't be a shortcut, it would be the way!"

MIME::Lite TLS Email Encryption - Perl v0.02 beta
 
'last' is a very useful command, one of those that once discovered, you wonder how you ever managed without it.

I am not sure which is the most efficient way to transfer files. The only obvious advantage is that the script can exit if the max size allowed is exceeded whereas loading the whole file could cause problems if a user decides to upload some ridiculous sized file and we know what users are like, don't we?

Keith
 
oh, as I understood things, your script doesn't process the file until the entire file is passed to it via STDIN.

I use CGI to parse uploaded files like so..
Code:
##################
# use CGI Module #
##################
use CGI;

# Set max upload size
$CGI::POST_MAX = 1024 * 12000;  # maximum upload filesize is 12MB

# Create new CGI.
my $cgi = new CGI;

# get filename
my $uploadname = $cgi->param('Filedata');      
      
# get file       
my $upload_filehandle = $cgi->upload('Filedata');

# create file        
open ( UPLOADFILE, '>', "$upload_dir/$filename" ) or showError("$!"); 
binmode UPLOADFILE; 
while ( <$upload_filehandle> ) { 
    print UPLOADFILE $_; 
}
    
close UPLOADFILE;
now this is a simplified version I have other query string params I parse and various validation, but in essence that's the method I use.

If I wrote a die command as the first line of the script, it still wouldn't return the error to the browser until the file had finished uploading.

Am I doing something wrong?

Is there a way to process the file 'in-transit' so to speak?

Your advice is appreciated.



"In complete darkness we are all the same, only our knowledge and wisdom separates us, don't let your eyes deceive you."

"If a shortcut was meant to be easy, it wouldn't be a shortcut, it would be the way!"

MIME::Lite TLS Email Encryption - Perl v0.02 beta
 
I use the CGI module for all of my form processing but it never ocurred to ne to use it for uploads. I have always done it 'my way' and up until this 1 user , it has always been reliable.

If it ain't broke ..........

Unfortunately this user is not very tech savvy so I would not be surprised if the problem was something really simple and not really a problem at all.

Keith
 
Well it seems that your way could be better if it allows 'in-transit' processing of the file as it is uploaded rather than waiitng for the entire file to be uploaded before it is processed.

I though this was a HTTP protocol thing , not the way i was processing the submitted data.

how are you getting the binary data from the post method if not using CGI?

I don't mean to hijack your thread, but would really appreciate your advice on how to process post data as it is being sent rather than after the fact!

As you say killing the upload the minute it exceeds acceptable size is good for the server and other users.

no point in having IO streams hoggin resources unneccesarily.




"In complete darkness we are all the same, only our knowledge and wisdom separates us, don't let your eyes deceive you."

"If a shortcut was meant to be easy, it wouldn't be a shortcut, it would be the way!"

MIME::Lite TLS Email Encryption - Perl v0.02 beta
 
This is the whole subroutine I use, it uses CGI to pass the file name and then uploads the file. Binmode is declared for the file handle but it works perfectly well without it.
Code:
sub upload_func_call{
	my $Message;
	my $MaxPicSize=2000000;
	my $query = new CGI;
	print $query->start_html(-title=>"UPLOAD PLEASE");
	my $fileID;
	unless($fileID = $query->param("FILEID")){
		$Message ='No File name specified.';
	}
	my $LENF=length($fileID);
	my $file_ext=uc(substr($fileID,$LENF-4,4));


	if($file_ext eq '.JPG'){
		my @pathName=split(/\\/,$fileID);
		my $NewFile='../originals/';
		$NewFile.=pop(@pathName);
		$NewFile=lc($NewFile);
		open(OPF,">$NewFile")|| print "Failed to open $NewFile";
		binmode(OPF) ;

		my $bytesread;
		my $buffer;
		my $sum_bytes=0;
		while($bytesread=read($fileID,$buffer,1024)){
			print OPF "$buffer";
			$sum_bytes=$sum_bytes+1024;
			if($sum_bytes >$MaxPicSize){
				close OPF;

			# Delete u/s picture

				unlink $NewFile;
				$Message .= "File is too large.<br>Maximum Allowed File Size = $MaxPicSize.<br>";
				last;
			}
		}
		close OPF;
		if(-e $NewFile){
			$Message .= "$fileID uploaded ok<br>";
		}else{
			$Message .= "File Not Uploaded<br>";
		}
	}else{
		$Message .= "NOT A JPG FILE";
	}
	return($Message);
}

Keith
 
and you are sure it definately parses the file 'in-transit' and not post upload?

what calls the sub? I take it the script is called via the action attribute of an HTML form?

"In complete darkness we are all the same, only our knowledge and wisdom separates us, don't let your eyes deceive you."

"If a shortcut was meant to be easy, it wouldn't be a shortcut, it would be the way!"

MIME::Lite TLS Email Encryption - Perl v0.02 beta
 
I just had a dig around in perldoc CGI , and i think i now know why I always felt that the script ran 'post' upload.

It's because of the way I use CGI.

I need to write my own file handling and not use the built in temp file mechanism that CGI uses by default!

Progress bars for file uploads and avoiding temp files
CGI.pm gives you low-level access to file upload management through a file upload hook. You can use this feature to completely turn off the temp file storage of file uploads, or potentially write your own file upload progress meter.

This is much like the UPLOAD_HOOK facility available in Apache::Request, with the exception that the first argument to the callback is an Apache::Upload object, here it's the remote filename.

1. $q = CGI->new(\&hook [,$data [,$use_tempfile]]);2.3. sub hook {4. my ($filename, $buffer, $bytes_read, $data) = @_;5. print "Read $bytes_read bytes of $filename\n";6. }The $data field is optional; it lets you pass configuration information (e.g. a database handle) to your hook callback.

The $use_tempfile field is a flag that lets you turn on and off CGI.pm's use of a temporary disk-based file during file upload. If you set this to a FALSE value (default true) then $q->param('uploaded_file') will no longer work, and the only way to get at the uploaded data is via the hook you provide.

If using the function-oriented interface, call the CGI::upload_hook() method before calling param() or any other CGI functions:

1. CGI::upload_hook(\&hook [,$data [,$use_tempfile]]);This method is not exported by default. You will have to import it explicitly if you wish to use it without the CGI:: prefix.

always helps when you understand what's actually going on!

I still don't get how I would write output back to the browser while the script is running to show the progress?

Any ideas?

"In complete darkness we are all the same, only our knowledge and wisdom separates us, don't let your eyes deceive you."

"If a shortcut was meant to be easy, it wouldn't be a shortcut, it would be the way!"

MIME::Lite TLS Email Encryption - Perl v0.02 beta
 
I think it would have to be something that keeps updating rather than normal HTML which just loads once and then ends the page.

I wonder, if the $bytesread var was written to a file whether a Flash progress bar on the resulting page could be utilised?

Keith
 
I currently use a similar plugin called 'uploadify'.

But it works without the need to call a backend script that reads the bytesread data.

It's worth checking out!

I just need to change the way I process the file so I can baulk nicely.


"In complete darkness we are all the same, only our knowledge and wisdom separates us, don't let your eyes deceive you."

"If a shortcut was meant to be easy, it wouldn't be a shortcut, it would be the way!"

MIME::Lite TLS Email Encryption - Perl v0.02 beta
 
Never heard of 'uploadify' but did some searching and foind it. I have never looked into JQuery so I know nothing about what is required.
I have looked at the files which come down in the zip and wonder why there are so many. What can possibly be going on in there which requires all this overhead?

I like the idea of using Flash for this as it is simply reporting progress. I will have a look into that during the next few weeks.
When uploading a single file a progress bar is not really required as upload only takes a few seconds. I have an app where users upload 20 or more images and this can take a long time to complete. A visual progress bar is just the thing I need.

Does that uploadify thingy handle multiple files?

Keith
 
Honestly, it's just the flash file , the JQuery file, CSS and your backend script, ther is a lot of junk in the zip file!

It does what it say on the tin and add a nice flash progress meter with little fuss.

I have used the uber uploader, which is nasty and has 10 times the amount of files and most are not superflous, even when using in a per environment you still have to use some of the PHP scripts!

Uberuploader vs Uploadify - Uploadify wins hands down!

"In complete darkness we are all the same, only our knowledge and wisdom separates us, don't let your eyes deceive you."

"If a shortcut was meant to be easy, it wouldn't be a shortcut, it would be the way!"

MIME::Lite TLS Email Encryption - Perl v0.02 beta
 
I have had a quick look at it but can't get it to work.
I can't spend long on it but for some reason the file input box and the browse button will not show if I assign an id to the control, all very odd.

Code:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "[URL unfurl="true"]http://www.w3.org/TR/html4/loose.dtd">[/URL]
<html>
<head>
<meta http-equiv="Content-Language" content="en-gb">
<meta http-equiv="Content-Type" content="text/html; charset=windows-1252">
<meta http-equiv="imagetoolbar" content="no">
<meta http-equiv="pragma" content="no-cache"> 
<meta name="description" content="Upload">
<meta name="keywords" content="Upload">
<title>Upload</title>
<link href="/uploadify/uploadify.css" type="text/css" rel="stylesheet" /> 
<script type="text/javascript" src="/uploadify/jquery-1.4.2.min.js"></script> 
<script type="text/javascript" src="/uploadify/swfobject.js"></script> 
<script type="text/javascript" src="/uploadify/jquery.uploadify.v2.1.4.min.js"></script> 
<script type="text/javascript"> 
$(document).ready(function() { 
$('#file_upload').uploadify({ 
'uploader' : '/uploadify/uploadify.swf', 
'script' : '/uploadify/uploadify.php', 
'cancelImg' : '/uploadify/cancel.png', 
'folder' : '/uploads', 
'auto' : true 
}); 
}); 
</script>
</head>
<body>

<!--
<input id="file_upload" name="file_upload" type="file">
-->
<input name="file_upload" type="file"><br>
<a href="javascript:$('#file_upload').uploadifyUpload()">Upload Files</a>
</body>
</html>

Keith
 
hmm very odd, I am using it slightly direrently as i have it as part of my AJAX application, but it seems to work fine...

Code:
setMess('<div id="uploader"><input id="file_upload" name="file_upload" type="file" /></div><div id="upload"><input title="Upload Image" type="button" value=" Upload " id="uploadbut" class="button" onclick="javascript:$(\'#file_upload\').uploadifyUpload();" /><input title="Cancel Image" class="button" type="button" value=" Cancel " onclick="cancelImage(\'Image Upload Cancelled!\');" /> Min 10K, Max 1OOK</div><div id="uploadWait">Uploading... Please Wait!</div>');
    
        $('#file_upload').uploadify({
        
            'uploader'          : '/path/to/flash/uploadify.swf',
            'script'            : '/path/to/upload/script.pl',
            'cancelImg'         : '/path/to/cancel/image/cancel.gif',
            'auto'              : false,
            'removeCompleted'   : true,
            'sizeLimit'         : 102400,
            'fileExt'           : '*.jpg;*.gif;*.png',
            'fileDesc'          : 'Image Files (.JPG, .GIF, .PNG)', 
            'multi'             : false, 
            'buttonText'        : 'Select Cover...',
            'wmode'             : 'transparent', 
            'scriptData'        : {'KEY1':'Val1','KEY2':Val2,'Key3':Val3},               
            'onComplete'        : function(event, ID, fileObj, response, data){changeImage(response,data);},
            'onOpen'            : function(event,ID,fileObj){uploadingFile();},
            'onCancel'          : function(event,ID,fileObj,data){cancelImage();},
            'onError'           : function(event,ID,fileObj,errorObj){errorUpload();}   
                 
        });
    }

Bassically setMess updates the DOM with that HTML and jobs a good'un.


"In complete darkness we are all the same, only our knowledge and wisdom separates us, don't let your eyes deceive you."

"If a shortcut was meant to be easy, it wouldn't be a shortcut, it would be the way!"

MIME::Lite TLS Email Encryption - Perl v0.02 beta
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top