Note that there are some explanatory texts on larger screens.

plurals
  1. POPHP / HTML uploading script not working right
    text
    copied!<p>I'm pretty new in PHP (don't have much HTML experience, too) and I have a question. I have a file upload system which consists of two PHP scripts (see below). In the file upload form a user selects file, saving folder and then uploads. Saving location can be chosen from a drop-down menu which consists of folders in uploading directory. But there's another option in the drop-down menu. If the user chooses the option <code>other</code> then a new field comes up (it's done by JavaScript) and the user can insert location manually (for example, a directory doesn't exist and the user wants to make a new one).</p> <p>But here's a problem: if the user chooses a folder which is predefined everything is OK, but if the user writes a directory name to the field then only folder called <code>folder</code> will be created. For example, if the user wants to upload a picture about a flower and no folder called <code>flower</code> exists then the user chooses <code>other</code> and writes <code>flower</code> into the box. Then upload button is clicked and basically PHP should make a new folder called <code>flower</code> and upload the picture there. But at the moment, PHP makes a folder called <code>folder</code> and for some reason the picture is not uploaded there (it's uploaded nowhere). </p> <p>My problems with the script:</p> <ol> <li>A new folder won't be created. I think it's because I have two values called "folder" but I don't know how to get it work that if "other" is selected then PHP only looks the last value.</li> <li>Even if the folder is created then a file won't be uploaded there for some reason.</li> </ol> <p>I would be really grateful if anyone finds the solution for my problem and helps me to fix it. And don't hesitate to ask for extra information because my English isn't the best and because of this some things can't be understood.</p> <p>Thanks in advance!</p> <p>1st file: <code>file-upload.php</code>:</p> <pre><code>&lt;html xmlns="http://www.w3.org/1999/xhtml"&gt; &lt;head&gt; &lt;meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" /&gt; &lt;title&gt;&lt;/title&gt; &lt;script language="JavaScript" type="text/javascript"&gt; &lt;!-- function Toggle(obj){ var val=obj.value; if (!obj.m){ obj.m=''; } if (!obj.m.match(val)){ obj.m+=','+val+','; } var hide=obj.m.split(','); for (var zxc0=0;zxc0&lt;hide.length;zxc0++){ if (document.getElementById(hide[zxc0])){ document.getElementById(hide[zxc0]).style.display='none'; } } var show=val.split(','); for (var zxc1=0;zxc1&lt;show.length;zxc1++){ if (document.getElementById(show[zxc1])){ document.getElementById(show[zxc1]).style.display=''; } } } //--&gt; &lt;/script&gt; &lt;/head&gt; &lt;body&gt; &lt;h1&gt;Upload&lt;/h1&gt; &lt;form action="upload.php" method="post" enctype="multipart/form-data"&gt;&lt;br&gt; &lt;label for="file"&gt;File:&lt;/label&gt; &lt;input type="file" name="file" id="file" /&gt; &lt;br&gt; Tüüp: &lt;select name="folder" value="folder" onchange="Toggle(this);"&gt; &lt;?php foreach(glob('/uploadfolder/*', GLOB_ONLYDIR) as $dir) { $dir = basename($dir); echo '&lt;option value="', $dir, '"&gt;', $dir, '&lt;/option&gt;'; } ?&gt; &lt;option value="folder"&gt;other&lt;/option&gt; &lt;/select&gt;&lt;br&gt; &lt;input id="folder" value="Create a new folder" style="display:none;"&gt; &lt;input type="submit" name="submit" value="Upload" /&gt; &lt;/form&gt; &lt;/body&gt; &lt;/html&gt; </code></pre> <p>2nd file: <code>upload.php</code>:</p> <pre><code>&lt;html xmlns="http://www.w3.org/1999/xhtml"&gt; &lt;head&gt; &lt;meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" /&gt; &lt;title&gt;&lt;/title&gt; &lt;/head&gt; &lt;body&gt; &lt;h1&gt;Upload&lt;/h1&gt; &lt;br&gt; &lt;?php $dirname = $folder = $_POST["folder"]; $filename = "/uploadfolder/" . "$dirname" . "/"; if (!file_exists($filename)) { mkdir("/uploadfolder/" . "$dirname", 0777); } if ((($_FILES["file"]["type"] == "image/gif") || ($_FILES["file"]["type"] == "image/jpeg") || ($_FILES["file"]["type"] == "image/pjpeg") || ($_FILES["file"]["type"] == "application/vnd.openxmlformats- officedocument.spreadsheetml.sheet")) &amp;&amp; ($_FILES["file"]["size"] &lt; 5120000)) { if ($_FILES["file"]["error"] &gt; 0) { echo "Return Code: " . $_FILES["file"]["error"] . "&lt;br /&gt;"; } else { echo "Name: " . $_FILES["file"]["name"] . "&lt;br /&gt;"; echo "Type: " . $_FILES["file"]["type"] . "&lt;br /&gt;"; echo "Size: " . ($_FILES["file"]["size"] / 1024) . " Kb&lt;br /&gt;"; echo "Tmp. file: " . $_FILES["file"]["tmp_name"] . "&lt;br /&gt;"; echo "Folder: " . $folder . "&lt;br /&gt;"; if (file_exists("/uploadfolder/$folder/" . $_FILES["file"]["name"])) { echo $_FILES["file"]["name"] . " already exists. "; } else { move_uploaded_file($_FILES["file"]["tmp_name"], "/uploadfolder/$folder/" . $_FILES["file"]["name"]); echo "Saved: " . "" . $_FILES["file"]["name"]; } } } else { echo "Upload failed"; } ?&gt; &lt;/body&gt; &lt;/html&gt; </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload