Jump to content

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

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.