Click here to Skip to main content
15,885,278 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
<?php
$name = (isset($_POST['project_name']) ) ? $_POST['project_name'] : '';

$fixedPath = "projects_data/";

if (file_exists("uploads/" . $_FILES["project_image"]["name"]))
      {
      echo $_FILES["project_image"]["name"] . " already exists. ";
      }
    else
      {
	  
      move_uploaded_file($_FILES["project_image"]["name"],
     "$fixedPath/$name/". $_FILES["project_image"]["name"]);
    echo "Stored in: " . "$fixedPath/$name/" . $_FILES["project_image"]["name"];
      }
?>

I have created a folder called projects_data in my root.When I submit my form I need to save the image into a sub-folder inside projects_data.I need to create a sub-folder dynamically with the folder name as "$name".How can I do this?Please help me out!
Posted
Comments
Janardhanam Julapalli 24-Nov-14 9:20am    
when I submit this, It is creating a folder with by $name and echo is producing with full path but the image is not stored in the required folder.

This is because PHP is "confusing" your "$name" to be a variable $name. Replace the else part with
PHP
move_uploaded_file($_FILES["project_image"]["name"],
    "$fixedPath/\$name/". $_FILES["project_image"]["name"]);
   echo "Stored in: " . "$fixedPath/\$name/" . $_FILES["project_image"]["name"];
 
Share this answer
 
Comments
Janardhanam Julapalli 25-Nov-14 4:37am    
will it create a new folder in the name of $name
Janardhanam Julapalli 25-Nov-14 5:02am    
$name is already creating the new folder but only the image is not stored in that path.
Oso Oluwafemi Ebenezer 25-Nov-14 7:51am    
I think I am misunderstanding your question! What is the path you want the image to be in e.g `c:/.../www/project/pictures`? And is this path already available or you want to create it through codes?
Janardhanam Julapalli 25-Nov-14 11:17am    
I have created a folder called "projects_data" inside www.When I submit my form,I am sending the value called project_name and an image with variables called $project and file upload.I want to create a folder inside "projects_data" whose name should be the $project.Inside this folder I need to store the image.
Then you have to first create the folder with the $name variable before moving the upload to it and another important error I was now is you are using $_FILES['project_image']['name'] in the move_uploaded_file function instead of $_FILES['project_image']['tmp_name'].
This should work!

PHP
$name = (isset($_POST['project_name']) ) ? $_POST['project_name'] : 'unnamed project';
 
$fixedPath = "projects_data/";

//make the directory
@mkdir($fixedPath."/".$name, 0777, true);

//Move the uploaded file 
if(file_exists($fixedPath. "/" . $name . "/" . $_FILES["project_image"]["name"]) && is_file($fixedPath. "/" . $name . "/" . $_FILES["project_image"]["name"]))
{
     echo $_FILES["project_image"]["name"] . " already exists. ";
}
else
{
    if(move_uploaded_file($_FILES["project_image"]["tmp_name"] /*see your error here*/,
    $fixedPath. "/" . $name . "/" . $_FILES["project_image"]["name"]))
	{
		echo "Stored in: " . $fixedPath. "/" . $name . "/" . $_FILES["project_image"]["name"];
	}
	else
	{
		echo "Error moving file";
	}
}
?>
 
Share this answer
 
PHP
$project = (isset($_POST['projectName']) ) ? $_POST['projectName'] : '';

$dirname = "C:\\\\Bitnami\\wampstack-5.4.33-0\\apache2\\htdocs\\projects_data\\";
mkdir($dirname.$project, 0777, true);


if (isset($_FILES["gallery_images"]["name"]))
    {
        move_uploaded_file($_FILES["gallery_images"]["tmp_name"], "$dirname/$project/" . $_FILES["gallery_images"]["name"]);
        $image = "projects_data/$project/" . $_FILES["gallery_images"]["name"];
    }

This will create a folder dynamically in the name of $project at the specified location.
Thanks for the help though.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900