Jump to content

[SOLVED] *Urgent Help* Upload Script


Dethman

Recommended Posts

Hi all the following is my upload script for images however it does not upload the image the $Err.="Avatar Successfully Uploaded!<BR />"; echo's out as if it worked but dosnt so here is the code,

 

PHP:

<?php
$uploadDir = "UPLDS"; //With a trailing slash
$allowedExtensions = array("jpg","jpeg","gif","png","tiff"); //What are the allowed extensions? You can add/remove any.

//Check if directory exists before executing anything
if(!is_dir($uploadDir)){
die("Failed to find upload directory, script was aborted.");
}

//It has been, start the upload process

//First we will check if an image has been selected
if(!isset($_FILES['image']['name'])){
	//Nothing is being uploaded

		}else{
	//They are uploading an image
	//Let us check for a valid extension first.

	$tmp_explode = explode(".", $_FILES['image']['name']); //We explode at the .

	//Now we check if the last element in the $tmp_expode array is in the $allowedExtensions array
	if(!in_array($tmp_explode[count($tmp_explode)-1], $allowedExtensions)){
		//Invalid extension
		$Err.="Invalid File!<BR />";
	}else{
		//It's a valid extension
		//Since it's valid, we'll do another check to see if the file name already exists
		$exists = false;
		foreach(glob($uploadDir . "*") as $files){
			if($_FILES['image']['name'] == str_replace($uploadDir, "", $files)){
				$exists = true;
			}
		}

		if($exists){
			//The name of the file exists, lets generate a random hash from the current microtime to add to the start
			$fileName = md5(microtime()) . "_" . $_FILES['image']['name'];
		}else{
			//The name is okay, keep the original
			$fileName = $_FILES['image']['name'];
		}

		//Now we can upload... We use move_uploaded_file() for this
		if(!move_uploaded_file($_FILES['image']['tmp_name'], $uploadDir . $fileName)){
			//Failed to upload! Why? Who knows.
				$Err.="An Internal Error Ocurred, Please Contact Admin with Error-Code: EP11056<BR />";
		}else{
			//It's done, make the path to file so you can store it in database. Or just $fileName.
			//I would recommend redirecting now as well, so they don't upload another image.
			$pathToFile = $uploadDir . $fileName;

			$query="UPDATE `Profiles` SET `Avatar` = '".$uploadDir."/".$fileName."' WHERE `OwnerID` = '".$id."'";
			$Res=mysql_query($query) or die("An Error Ocurred:".mysql_error());

			if( isset($Res) ){
			$Err.="Avatar Successfully Uploaded!<BR />";
			}
		}

	}
}

?>

 

Thanx for the Help,

Dethman

Link to comment
https://forums.phpfreaks.com/topic/150679-solved-urgent-help-upload-script/
Share on other sites

Archived

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

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.