Register FAQ / Rules Forum Spy Search Today's Posts Mark Forums Read
Go Back   MacRumors Forums > Special Interests > Visual Media > Web Design and Development

Reply
 
Thread Tools Search this Thread Display Modes
Old Mar 21, 2009, 11:38 AM   #1
Brendon Bauer
macrumors 6502
 
Join Date: May 2007
Location: Good 'ol USofA
File Upload script hangs on Safari?

I have taken bits and pieces of an upload script to upload files to my server, but it seems like every browser works fine except Safari. It's like a 50/50 chance that Safari will hang or upload it successfully. I'm dealing with 10-20mb files and so it's not fun to sit around for 5 minutes and then realize it wasn't working. I've tested with 200kb files so I know in a matter of seconds whether it worked or not. When it doesn't work and just is "loading" if I click upload again, it will work. I was wondering if maybe you could spot my problem in the code:

Here is the form:
Code:
<html>
<title>File Upload</title>
<script type="text/javascript">
<!--
	function toggle_visibility(id) {
	   var e = document.getElementById(id);
	   if(e.style.display == 'block')
		  e.style.display = 'none';
	   else
		  e.style.display = 'block';
	}
-->
</script>
</head>
<body>

<h2>File Upload</h2>

<?php
	error_reporting(0);
	$errors = array();
	$mm = $_GET['mm'];
	$dd = $_GET['dd'];
	$yy = $_GET['yy'];
	$date = $mm."/".$dd."/".$yy;
?>
		
<br /><p>Date: <?php echo $mm."/".$dd."/".$yy; ?><br /><br /><i>(The audio sermon must be an mp3 file under 20MB in size)</i></p>

<p><form enctype="multipart/form-data" action="audio_sermon_2a.php" method="POST">

	<input type="hidden" name="MAX_FILE_SIZE" value="21000000" />
	<input type="hidden" name="year" value="<?php echo "20".$yy; ?>" />
	<input type="hidden" name="mm" value="<?php echo $mm; ?>" />
	<input type="hidden" name="dd" value="<?php echo $dd; ?>" />
	<input type="hidden" name="yy" value="<?php echo $yy; ?>" />

	Choose a sermon file to upload: <input type="file" name="file" size="30" /><br /><br /><br />

	<div class="buttons"><button type="submit" name="submit" onclick="toggle_visibility('loadinggif');" class="positive"><img src="/images/icons/tick.png" alt="" /> Upload Sermon</button></div><br /><br /><br />
</form></p>
<div id="loadinggif" style="margin: 0; padding: 0; display: none;"><p><table border="0"><tr><td><img src="/images/loading.gif" alt="loading..." /></td><td valign="middle">&nbsp;&nbsp;Uploading... please be patient...</td></tr></table></p></div>

</body>
</html>
That page takes a date passed to it via get and uses it to choose the location and filename for uploading.

Here is the actual uploader:
Code:
<?php
// ==============
// Configuration
// ==============
error_reporting(0);
$uploaddir = "path_to_location_hidden_just_because/".$_POST['year']; // Where you want the files to upload to - Important: Make sure this folders permissions is 0777!
$allowed_ext = "mp3"; // These are the allowed extensions of the files that are uploaded
$max_size = "21000000"; // 50000 is the same as 50kb
$max_height = ""; // This is in pixels - Leave this field empty if you don't want to
$max_width = ""; // This is in pixels - Leave this field empty if you don't want to upload images
// Check Entension
$extension = pathinfo($_FILES['file']['name']);
$extension = $extension['extension'];
$allowed_paths = explode(", ", $allowed_ext);
for($i = 0; $i < count($allowed_paths); $i++) {
	if ($allowed_paths[$i] == "$extension") {
		$ok = "1";
	}
}

// Check File Size
if ($ok == "1") {
	if($_FILES['file']['size'] > $max_size) {
	print "<font color=\"red\">The sermon is too big! It can't be bigger than 20mb. Try making it smaller, <a href=\"#\" onclick=\"history.go(-1);return false;\">go back</a>, and upload it again.</font>";
	exit;
	}

	// Check Height & Width
	if ($max_width && $max_height) {
		list($width, $height, $type, $w) = getimagesize($_FILES['file']['tmp_name']);
		if($width > $max_width || $height > $max_height) {
			print "File height and/or width are too big!";
			exit;
		}
	}

	// The Upload Part
	if(is_uploaded_file($_FILES['file']['tmp_name'])) {
		move_uploaded_file($_FILES['file']['tmp_name'],$uploaddir.'/'.$_POST['mm'].".".$_POST['dd'].".".$_POST['yy'].".mp3");
	
		print "<meta http-equiv=\"Refresh\" content=\"0; url=path_to_page_on_successful_upload?mm=".$_POST['mm']."&dd=".$_POST['dd']."&yy=".$_POST['yy']."\">";
		
	}
	
} else {
	print "<font color=\"red\">You must select a file to upload and it must be an mp3 file (ends in '.mp3'). Fix this, <a href=\"#\" onclick=\"history.go(-1);return false;\">go back</a>, and try uploading again.</font>";
}
?>
Unfortunately I can't allow access to this page for security reasons so you can't really test it. Just hoping you can see the problem . I removed a couple of file locations above...

Thanks.
__________________
Retina Pro 15", iPhone 5

Last edited by Brendon Bauer; Mar 21, 2009 at 11:44 AM.
Brendon Bauer is offline   0 Reply With Quote
Old Mar 24, 2009, 07:05 PM   #2
PCheese
macrumors member
 
Join Date: Aug 2004
It's a bug in Mac OS X: https://bugs.webkit.org/show_bug.cgi?id=5760

A solution is to add an AJAX call when you submit the form:

http://blog.airbladesoftware.com/200...ging-in-safari

http://groups.google.com/group/jquer...395b7ab510dd5d
__________________
coming soon: iMac G5 20" iSight 1.5GB RAM
ampsig: the coolest dynamic forum signature ever!
(it even comes with a Dashboard widget!)
PCheese is offline   0 Reply With Quote
Old Mar 24, 2009, 09:00 PM   #3
Brendon Bauer
Thread Starter
macrumors 6502
 
Join Date: May 2007
Location: Good 'ol USofA
Interesting... Thanks for the response!

I tried adding
Code:
<script type="text/javascript">
/* A pretty little hack to make uploads not hang in Safari. Just call this
 * immediately before the upload is submitted. This does an Ajax call to
 * the server, which returns an empty document with the "Connection: close"
 * header, telling Safari to close the active connection. A hack, but
 * effective. */
function closeKeepAlive() {
  if (/AppleWebKit|MSIE/.test(navigator.userAgent)) {
    new Ajax.Request("/ping/close", { asynchronous:false });
  }
}
</script>
within the head section of the page with the form, and
Code:
onsubmit="closeKeepAlive();"
to the form tag, making it
Code:
<form enctype="multipart/form-data" action="audio_sermon_2a.php" onsubmit="closeKeepAlive();" method="POST">
This didn't seem to fix the problem though... It will still hang on about 50% of my attempts to upload a small file. Did I implement the "hack" or fix wrong? Or is that what they were implying on airbladesoftware website you gave as an example? As a note, I'm using Safari 4 beta, but I did uninstall the beta earlier to test the latest nonbeta version of Safari with the form (not with this hack applied, though) and it hung as well. I assume it shouldn't make much of a difference with the beta version? In other words I haven't tested this hack with nonbeta Safari, but I have tested the form w/out the hack with nonbeta Safari (which occasionally hung as well).

Any idea what's up?
__________________
Retina Pro 15", iPhone 5
Brendon Bauer is offline   0 Reply With Quote
Old Dec 1, 2009, 09:02 AM   #4
Jay Francis
macrumors newbie
 
Join Date: Dec 2009
Brendon - were you able to fix the problem?

I'm heading down the same path, and haven't worked it out yet.

Thanks,
Jay
Jay Francis is offline   0 Reply With Quote
Old Dec 1, 2009, 09:16 AM   #5
Brendon Bauer
Thread Starter
macrumors 6502
 
Join Date: May 2007
Location: Good 'ol USofA
Jay,

I never did find a fix to the problem... so instead I put a warning note next to the uploader for those using safari. Sorry I couldn't help you!

Brendon
__________________
Retina Pro 15", iPhone 5
Brendon Bauer is offline   0 Reply With Quote
Old Dec 1, 2009, 09:18 AM   #6
Jay Francis
macrumors newbie
 
Join Date: Dec 2009
Ok - thanks.

I'll post if I eventually get something to work reliably... (ugh)

--Jay
Jay Francis is offline   0 Reply With Quote
Old Dec 1, 2009, 03:48 PM   #7
Jay Francis
macrumors newbie
 
Join Date: Dec 2009
Got it to work.

A couple of noob mistakes:

1) Ajax.Request is found in prototype.js (Google it), and needs to be included.
2) "/ping/close" needs to be an empty file accessible by the server. I'm using Django, so I put it in a directory along with other statically served files. My path isn't "/ping/close", so I needed to change it (e.g. "/media/close").

Now when I watch the TCP packets between the browser and the server, I can see the connection close. Safari file uploads seem to be working properly (it's an intermittent problem, so I wouldn't put money on it yet...).

Hope this helps,
Jay
Jay Francis is offline   0 Reply With Quote
Old May 3, 2013, 09:04 AM   #8
alnaqba
macrumors newbie
 
Join Date: Jun 2010
Safari Hangs on PHP upload script

I am having the same problem. I have a very simple php upload script but after one or two iterations of the upload, the page just sits there.
If I click "submit" again it completes.
This script works in Firefox without failure, haven't tested anything else.
But I see there are more than one reports of this same behavior.
If anyone solves it I'd like to know.

Here's the whole script:
Don't laugh, I'm just at testing stage when I noticed this bug with Safari.
If it's my script, PLEASE let me know.

Thank you

<?php

session_start();


$target_path = "/var/www/uploads/";
$target_path = $target_path . basename( $_FILES['uploadedfile']['name']);

if(move_uploaded_file($_FILES['uploadedfile']['tmp_name'], $target_path)) {
//echo "The file ". basename( $_FILES['uploadedfile']['name']). " has been uploaded";
} else{
// echo "There was an error uploading the file, please try again!";
}


$_SESSION['thefileweuploaded'] = basename( $_FILES['uploadedfile']['name'] );
$_SESSION['project_description'] = $_POST['project_description'];
$_SESSION['job_details'] = $_POST['job_details'];

$duhname = $_SESSION['thefileweuploaded'];

//echo "Updating your upload: Don't click anything until this message changes!";


// redirect page to upload confirmation page
if( $_SERVER['SERVER_ADDR'] == "::1" ) {
$url = "http://localhost/index.html?page=upload/upload_confirmation.html&uploadedfile=$duhname";
} else {
$url = "http://" . $_SERVER['SERVER_ADDR'] . "/index.html?page=upload/upload_confirmation.html&uploadedfile=$duhname";
}
header("Location: $url");

?>
alnaqba is offline   0 Reply With Quote

Reply
MacRumors Forums > Special Interests > Visual Media > Web Design and Development

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump

Similar Threads
thread Thread Starter Forum Replies Last Post
Yahoo Mail hangs on Safari after updates Hatori Honzi OS X 3 Oct 16, 2011 12:32 PM
Angry Birds file SSH'd now hangs on level up B737 Jailbreaks and iOS Hacks 3 May 10, 2011 11:35 AM
iPhone file uploader? lynkynpark86 Jailbreaks and iOS Hacks 4 Jan 24, 2011 01:13 PM
FileAid on the AppStore: View files uploaded by DiskAid on iPhone (USB and Wifi) architek204 iPhone and iPod touch Apps 5 Mar 4, 2009 11:14 AM
File upload script problem on OS X server moron Mac Programming 1 Jun 7, 2006 03:48 PM


All times are GMT -5. The time now is 08:46 AM.

Mac Rumors | Mac | iPhone | iPhone Game Reviews | iPhone Apps

Mobile Version | Fixed | Fluid | Fluid HD
Copyright 2002-2013, MacRumors.com, LLC