strange image upload problem


#1

hi everybody,
I need help on a little problem which drives me almost mad.
I have created a more or less simple little script to upload images on a server and store name and path of the image in a mysql database.
1.question
it works perfect with safari but with any other browser no way, why ???
there is now error message, just the image and its file name are not stored ( mozilla on mac, ie6 on windows )

here comes the code:

// pick a file extension
if (eregi('?mage/p?jpeg(;.*)?$',
	$_FILES['uploadedfile']['type']) )
	$extension = '.jpg';
	else $extension = '.gif';
	
// the complete path/filename
if ($_FILES['uploadedfile']['name'] == "")
{
 $filename = "images/1x1.gif";
}else{
 $filename = "images/" . time() . $_FILES['uploadedfile']['name'];
}


// Copy the file 
if (is_uploaded_file($_FILES['uploadedfile']['tmp_name']) and 
	copy($_FILES['uploadedfile']['tmp_name'], $filename)) {
	echo("<p>File stored successfully as $filename.</p>");
}else{
	//echo("<p>Could not save as $filename.</p>");
	}

the form looks like that:

<form action="<?=$PHP_SELF?>" method="post" enctype="multipart/formdata">
	 Headline:<br>
	  	<input name="newshead" type="text" value="" size="45" maxlength="45"wrap>
		</input>
		<br><br>Text:<br>
		<textarea name="newstext" rows="15" cols="45" wrap>
		</textarea>
		<br><br>URL: <br>
	  	<input name="URL"  type="text" value=" " size="45" maxlength="45"wrap>
		</input>
		<br><br>
		<br>Bild auswaehlen (maximal 420x300 pixel):
		<input type="hidden" name="MAX_FILE_SIZE" value="100000">
		<br><br>
		<input type="file" name="uploadedfile" />
		<br><br>
  1. question (you might call me silly for this one)
    I didn’t manage to store the images in any other folder than “images”,
    something like $filename = “level1/level2/images/” . time() . $_FILES[‘uploadedfile’][‘name’]; just didn’t work.
    looks like the image folder has to be in the same folder as the script itsself !?
    did I forget about any simple html rule or is there any other reason?
// the complete path/filename
if ($_FILES['uploadedfile']['name'] == "")
{
 $filename = "images/1x1.gif";
}else{
 $filename = "images/" . time() . $_FILES['uploadedfile']['name'];
}

thanks a lot for your help
stephan


#2

Not sure about the Safari problem as I have never used it, but with regards to your second question, to upload to any directory, try adding your full server root path to the copy function:

copy($_FILES['uploadedfile']['tmp_name'], "/home/serverpath/public_html/level/level1/" . $filename))

That should work ok.


#3

There is an error in the form itself. enctype should be multipart/form-data, not multipart/formdata.