Jump to content

Uploading Files


JSHINER

Recommended Posts

$directory = HOME_DIR . 'folder/photos/';

function uploadFile($postName, $newName, $moveTo){
if(isset($_FILES[$postName]) && $_FILES[$postName]['name']){
	$type = substr($_FILES[$postName]['name'], strrpos($_FILES[$postName]['name'], "."));
	if(file_exists($moveTo.$newName.$type)){
		$t = 1;
		$name = $newName.'_'.$t;
		while(file_exists($moveTo.$name.$type)){
			$t++;
			$name = $newName.'_'.$t;
		}
		$newName = $name;
	}
	if(move_uploaded_file($_FILES[$postName]['tmp_name'], $moveTo.$newName.$type)){
		return $newName.$type;
	}else{
		return false;
	}
}else{
	return false;
}
}

if(isset($_SESSION['uploaded'])){
	$uploaded = $_SESSION['uploaded'];
}else{
	$uploaded = array();
}
foreach($_FILES AS $k=>$f){
	if(substr($k, 0, 4) == 'file'){
		$newName = $_POST['id'];
		if(isset($_FILES[$k]['name']) && $_FILES[$k]['name']){
			$upload = uploadFile($k, $newName, $directory);
			if(!$upload){
				$errors[$k] = 'File could not be uploaded.';
			}else{
				$uploaded[] = $upload;
			}
		}
	}
}

 

If $id = 505 this currently uploads to "folder/photos/505.jpg" - how can I get it to upload to "folder/photos/505/505.jpg" , "folder/photos/505/505_1.jpg"  ?

Link to comment
https://forums.phpfreaks.com/topic/96578-uploading-files/
Share on other sites

I think maybe I didn't make sense in my first post.

 

What I want is for the user to upload a photo, and there will be an id that they're uploading under. So if the page is "upload.php?id=550", and this is the first photo uploaded for that id, it will create a folder (/550/), then put the photo in there. Then if they upload a second photo, it will see there is a folder already, and will just put the photo in the 550 folder.

 

Does that make sense?

Link to comment
https://forums.phpfreaks.com/topic/96578-uploading-files/#findComment-495435
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.