Jump to content

example script for IMAGE upload


arden

Recommended Posts

function getFtype($str) {	$i = strrpos($str,".");	if (!$i) { return ""; }	$l = strlen($str) - $i;	$ext = substr($str,$i+1,$l);	return $ext;

can any1 explain me this part?..

Link to comment
Share on other sites

That function just attempts to determine the file's content-type using its extension.

Link to comment
Share on other sites

That function just attempts to determine the file's content-type using its extension.
thanks synook..btw..can you help me with this code?..
<?phpdefine ("MAX_SIZE","100"); function getExtension($str) {	$i = strrpos($str,".");	if (!$i) { return ""; }	$l = strlen($str) - $i;	$ext = substr($str,$i+1,$l);	return $ext;}$errors=0;if(isset($_POST['Submit'])) {	$image=$_FILES['image']['name'];if ($image) {	$filename = stripslashes($_FILES['image']['name']);	$ftype = getExtension($filename);	$ftype = strtolower($ftype);if (($ftype != "jpg") && ($ftype != "jpeg") && ($ftype != "png") && ($ftype != "gif")) {	echo '<h1>Unknown extension!</h1>';	$errors=1;}else{$size=filesize($_FILES['image']['tmp_name']);if ($size > MAX_SIZE*1024){	echo '<h1>You have exceeded the size limit!</h1>';	$errors=1;}$image_name= time().'.'.$ftype; // IWANT TO CHANGE THIS IMAGE NAME, HOW?$newname="images/".$image_name;$copied = copy($_FILES['image']['tmp_name'], $newname);if (!$copied) {	echo '<h1>Copy unsuccessfull!</h1>';	$errors=1;}}}} if(isset($_POST['Submit']) && !$errors)  { 	echo "<h1>Image Uploaded!</h1>"; } ?><form name="newad" method="post" enctype="multipart/form-data"  action=""><br/><table> 	<tr><td><input type="file" name="image"></td></tr> 	<tr><td><input name="Submit" type="submit" value="Upload image"></td></tr> </table>	 </form>

I want to change the $image_name = time().'.'.$ftype;when i change it to $image_name = 'upload.'.'.$ftype;it OVERWRITES the first image that i uploaded.hope u get what i mean.thanks..

Link to comment
Share on other sites

That's because the name never changes. You need to have some sort of unique identifier (e.g. the timestamp in the original code) so the filenames are always different.

Link to comment
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...