Jump to content

File upload function


wrathican

Recommended Posts

hey.

 

im trying to create a file upload system for images

 

i want to upload the file to a temp dir. and then create a thumbnail and store that in a new directory then delete the old tmp dir file on success.

 

so far i only have the file upload function. i guess i kinda need to return the destination of the file so that my thumbnail function will be able to find it.

 

i know that the move_uploaded_file only returns false if thyere is an error. so would my code below return the newfile location on success?

 

here is the code i have at the moment

 

<?php
$tmpImg = '../images/temp/';
$oldFile = $_FILES['uploadedfile']['tmp_name'];

function uploadFile ($oldFile, $tmpDir) {

//extensions array
$validExt = array ('jpg', 'jpeg', 'png');

//get the file ext
$ext = strrchr($oldFile, ".");

//check the file ext against the array
if(in_array($ext, $validExt)) {
	//needle is in haystack
	//process upload

	//rename the file
	$newName = md5(rand() * time()) . $ext;

	//new file dest
	$destFile = $tmpDir . $newName;

	//move the file to tmpDir
	$result = move_uploaded_file($oldfile, $destFile);
	if($result == false) {
		//there was an error uploading
		exit;
	}else{
		//file was uploaded
		return $destFile;
	}

}else{
	//needle is not in haystack. exit
	return false;
}

}

uploadFile($oldFile, $tmpImg);

?>

 

 

Link to comment
https://forums.phpfreaks.com/topic/103387-file-upload-function/
Share on other sites

any ideas?

 

heres a newer version of the function:

<?php
function uploadFile ($oldFile, $tmpDir) {

//extensions array
$validExt = array ('jpg', 'jpeg', 'png');

//get the file ext
$ext = strrchr($userFile, ".");

//check the file ext against the array
if(in_array($ext, $validExt)) {
	//needle is in haystack
	//process upload

	//rename the file
	$newName = md5(rand() * time()) . $ext;

	//new file dest
	$destFile = $tmpDir . $newName;

	//move the file to tmpDir
	$result = move_uploaded_file($oldfile, $destFile);
	if($result == false) {
		//there was an error uploading
		return false;
		exit;
	}else{
		//file was uploaded
		return $destFile;
	}

}else{
	//needle is not in haystack. exit
	return false;
	exit;
}

}
?>

Link to comment
https://forums.phpfreaks.com/topic/103387-file-upload-function/#findComment-530185
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.