PHP - Create a folder and save an image to that folder? Then display that photo?


December 2018


12.9k time


This seems like a big question, and it is, but the majority is done but I'm lost in actually getting where I want to be.

I have the upload form ready and it works, it saves it to a folder named upload, but I need it to create a folder depending on the hotel name that is saved in my session. This is displayed on the page to tell users what session they are on. So now when they save I need it to create the folder named after it to save it to that directory. So here is the code for the HTML :

<span class="prettyFile">
                                    <form id="form" action="assets/php/upload.php" method="post" enctype="multipart/form-data">  
                                        <div class="fileupload fileupload-new" data-provides="fileupload">
                                          <div class="input-append">
                                            <div class="uneditable-input span3" style="margin-top: -3px;"><i class="icon-file fileupload-exists"></i> <span class="fileupload-preview"></span></div><span class="btn btn-file btn-success"><span class="fileupload-new">Upload File</span><span class="fileupload-exists">Change</span><input type="file" name="file" /></span><a href="#" class="btn fileupload-exists btn-success" style="margin-top: -3px;" data-dismiss="fileupload">Remove</a>
                                            <button type="submit" name="submit" class="btn btn-primary fileupload-exists" value="Upload"><i class="icon-cloud-upload"></i> Upload</button>

And here is the PHP:

$allowedExts = array("gif", "jpeg", "jpg", "png");
$temp = explode(".", $_FILES["file"]["name"]);
$extension = end($temp);
if ((($_FILES["file"]["type"] == "image/gif")
|| ($_FILES["file"]["type"] == "image/jpeg")
|| ($_FILES["file"]["type"] == "image/jpg")
|| ($_FILES["file"]["type"] == "image/pjpeg")
|| ($_FILES["file"]["type"] == "image/x-png")
|| ($_FILES["file"]["type"] == "image/png"))
&& ($_FILES["file"]["size"] < 10000000)
&& in_array($extension, $allowedExts))
    if (file_exists("upload/". $_SESSION['curHotelName'] . "/" . $_FILES["file"]["name"]))
          echo "<script language='javascript'>\n";
          echo "alert('This file exists! Please select a different file.'); window.location.href='';";
          echo "</script>\n";
          "upload/". $_SESSION['curHotelName'] . "/" . $_FILES["file"]["name"]);
          echo "<script language='javascript'>\n";
          echo "alert('Success!'); window.location.href='';";
          echo "</script>\n";

I'm still messing with it but if someone can bump me in the right direction that would be awesome! Thanks guys!

2 answers


Вы должны создать каталог , прежде чем вы можете перемещать файлы в него. Вы можете использовать функцию file_exists проверить на каталог , а затем создать его в необходимости:

if (!file_exists('path/to/directory')) {
  mkdir('path/to/directory', 0777, true);

Вы можете предпочесть другие разрешения.


Ajax может помочь вам;) Сделайте один сценарий - для загрузки Второй - для проверки файла в папке и если она существует - отобразить

п.с. вы не решить эту проблему с помощью PHP только извините за плохой Engl