-1

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?

Isaac Manzi
  • 36
  • 1
  • 9
  • I'm not entirely sure what the document root is, however i'm using a stock install of xampp if that helps at all; it's not online at all yet. – Isaac Manzi May 29 '18 at 23:43
  • possible duplicate https://stackoverflow.com/questions/21035320/move-uploaded-file-unable-to-move-file-from-tmp-to-dir – Plixxer May 29 '18 at 23:46
  • I've tried the fixes in that question: adding the @ for the move_uploaded files; and using directory seperator. however I get the issue: Notice: Undefined index: p in C:\xampp\htdocs\newPage.php on line 10 Error -htmlUpload; which isn't right either. – Isaac Manzi May 29 '18 at 23:48
  • You shouldn't put @ anywhere since it will stop errors from appearing. You need errors to show you what's wrong. – hungrykoala May 30 '18 at 00:04
  • Well that's really cool to know, but either way, the fixes described in that question didn't work for me. – Isaac Manzi May 30 '18 at 00:05
  • Is this `$pageName` directory created? – hungrykoala May 30 '18 at 00:25
  • Yeah, the directory is created – Isaac Manzi May 30 '18 at 00:32

2 Answers2

0
should be used this code.


    $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['pagePHPUpload']['tmp_name'], $filepathPHP ))
        {
          $stmt = $conn->prepare("INSERT INTO pages (title, htmlDir, phpDir) VALUES (?,?,?)");
          $stmt->bind_param($pageName, $filepathHTML, $filepathPHP);
          $stmt->execute();
        }
        else{
          echo "Error -phpUpload";
        }
      }
      else{
        echo "Error -htmlUpload";
      }
    }
    else{
      echo "Error -pagename";
    }
0

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']);
Isaac Manzi
  • 36
  • 1
  • 9