I'm quite new to PHP, i'm trying to make a form to allow a user to upload a html file, a php file and give it a title.
It should use the title to create a new folder to move the two files to. Currently it creates the directory with no issues, however it cannot move the uploaded file from the tmp directory. and gives me the following error:
Warning: move_uploaded_file(): Unable to move 'C:\xampp\tmp\php2152.tmp' to 'C:\xampp\htdocs\var\uploads\pages\Instagram' in C:\xampp\htdocs\newPage.php on line 15 Error -htmlUpload
heres my HTML form:
<form action="newPage.php" method="post" enctype='multipart/form-data'>
<label for="pageName">Enter Page Name: </label>
<input type="text" name="pageName" id="pageName" />
<br />
<label for="pageHTMLUpload">HTML File: </label>
<input type="file" name="pageHTMLUpload" id="pageHTMLUpload" />
<br />
<label for="pagePHPUpload">PHP File: </label>
<input type="file" name="pagePHPUpload" id="pagePHPUpload" />
<br />
<center>
<button class="btn btn-dark" type="submit">Submit</button>
</center>
and heres the corresponding php:
$dir = getcwd() . '/var/uploads/pages/'; $pageName = $dir . $_POST['pageName']; if(mkdir($pageName, 0777, true)){ $filepathHTML = $pageName . basename($_FILES['pageHTMLUpload'['name']]); $filepathPHP = $pageName . basename($_FILES['pagePHPUpload']['name']); if(move_uploaded_file($_FILES['pageHTMLUpload']['tmp_name'], "$filepathHTML")){ if(move_uploaded_file($_FILES['pageHTMLUpload']['tmp_name'], "$filepathHTML")){ $stmt = $conn->prepare("INSERT INTO pages (title, htmlDir, phpDir) VALUES (?,?,?)"); $stmt->bind_param('sss', $pageName, $filepathHTML, $filepathPHP); $stmt->execute(); } else{ echo "Error -phpUpload"; } } else{ echo "Error -htmlUpload"; } } else{ echo "Error -pagename"; }
I've read that this issue can be caused by incorrect permissions, however I can successfully upload files from another script into a different folder within the var/uploads directory. Is this to do with posting two files at the same time?
edit: So I've been adding a few echos to see whats happening; and it seems to be an issue with the basename.
when echoing the result of
$filepathHTML = $pageName . basename($_FILES['pageHTMLUpload'['name']]);
$filepathPHP = $pageName . basename($_FILES['pagePHPUpload']['name']);
I get:
HTML:C:\xampp\htdocs/var/uploads/pages/Instagram/ PHP:C:\xampp\htdocs/var/uploads/pages/Instagram/Gallery.php
From the looks of it, it is trying to move a tmp file into pages as the Instagram directory which doesn't work, however the PHP contains the basename of the file, and i'm assuming this is what HTML should do.
Both of my filepath variables are defined in the same process, why does the HTML one not have the filename attached?
The issue was very a very simple misplaced bracket.
Old code including error:
$filepathHTML = $pageName . basename($_FILES['pageHTMLUpload'['name']]);
$filepathPHP = $pageName . basename($_FILES['pagePHPUpload']['name']);
New code without error:
$filepathHTML = $pageName . basename($_FILES['pageHTMLUpload']['name']);
$filepathPHP = $pageName . basename($_FILES['pagePHPUpload']['name']);