0

i'm creating in my localhost (files will not be published in remote server), a web app where i add some features i need. One of them is upload a wav file, who i want to upload into a different folder for each upload. I used that code that works with normal folder. I edited it in add some options i need (remove blank space in file name etc.), but when i put the "$folderUuid" into the "move_upload_file", the folder is created, but the file isn't uploaded into.

Another feature that i won't be able to understand where to add is to create a uuid folder only when a file is uploaded, not every time the page is refreshed. I read that discussion PHP File Upload Creating Directory and understand that i have to use the $_SESSION, but my code doesn't work.

Here is the php code:

if(isset($_POST['submit']))
{
$allowedExts = array("wav");
$fileName =   $_FILES['file']['name'];
$extension = substr($fileName, strrpos($fileName, '.') + 1); 

 if(!is_dir("inputFiles/". $_SESSION["folder"] ."/")) {
    mkdir(uniqid('inputFiles/'), 0700). $_SESSION["folder"] ."/" ;

}
if(in_array($extension, $allowedExts))
  {
  if ($_FILES["file"]["error"] > 0)
    {
     echo "Return Code: " . $_FILES["file"]["error"] . "<br />";
    }
  else
    {
    echo "Name: " . $_FILES["file"]["name"] . "<br />";
    echo "File kind: " . $_FILES["file"]["type"] . "<br />";
    echo "Size: " . ($_FILES["file"]["size"] / 1024) . " Kb<br />";

    if (file_exists("inputFiles/" . $_FILES["file"]["name"]))
      {
        echo $_FILES["file"]["name"] . " file già esistente. ";
      }
    else
      {
      move_uploaded_file($_FILES["file"]["tmp_name"], $folderUuid . str_replace(" ", "",$_FILES["file"]["name"])); 
      print "<br />";
      echo "Saved in: " . "inputFiles/" . $_FILES["file"]["name"];
      print "<br />";
      }
    }
  }
else
  {
  echo "Invalid file";
  }
}

Here is the html code:

<form method="post"  enctype="multipart/form-data" >

<label for="file"><span>Filename:</span></label>

<input type="file" name="file" id="file" /> 

<br />
<input type="submit" name="submit" value="Submit" />

Hope someone can help me to fix that problems. Kind Regards Brus

1 Answer 1

0

Try this code

if(isset($_POST['submit']))
{

$file_name = $_FILES['file']['name'];
$tmp_file = $_FILES['file']['tmp_name'];

 //create directory from the file
 $temp = explode('.', $file_name);
 $dir = $temp[0];
 $extension = strtolower($temp[1]);
 $allowedExts = array("wav");

if(!is_dir('inputFiles/'.$dir)){
    mkdir("inputFiles/$dir",0700);
}
if(in_array($extension, $allowedExts))
{
 if ($_FILES["file"]["error"] > 0)
{
 echo "Return Code: " . $_FILES["file"]["error"] . "<br />";
 }
 else
 {
echo "Name: " . $_FILES["file"]["name"] . "<br />";
echo "File kind: " . $_FILES["file"]["type"] . "<br />";
echo "Size: " . ($_FILES["file"]["size"] / 1024) . " Kb<br />";

if (file_exists("inputFiles/" . $_FILES["file"]["name"]))
  {
    echo $_FILES["file"]["name"] . " file già esistente. ";
  }
else
  {
  move_uploaded_file($tmp_file, "inputFiles/".$dir."/".$file_name); 
  print "<br />";
  echo "Saved in: " . "inputFiles/$dir/" . $_FILES["file"]["name"];
  print "<br />";
  }
  }
  }
   else
 {
   echo "Invalid file";
  }

 }
Sign up to request clarification or add additional context in comments.

1 Comment

Not create a uuid but a folder with name as filename, but it help anyway. Thanks a lot. Regards

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.