1

I keep getting this error and I do not know why.

Upload form

<?php
require_once 'processor/dbconfig.php';

if(isset($_POST['submit']))
{
    if($_FILES['image']['name'])
    {
      $save_path="newimages"; // Folder where you wanna move the file.
      $myname = strtolower($_FILES['image']['tmp_name']); //You are renaming the file here
      move_uploaded_file($_FILES['image']['tmp_name'], $save_path.$myname); // Move the uploaded file to the desired folder
    }

    $hl = $_POST['headline'];
    $fd = $_POST['feed'];
    $st = $_POST['story'];
    $tp = $_POST['type'];

    if($user->send($h1,$fd,$st,$tp,$save_path,$myname))
    {
        echo 'Success';
        $user->redirect("input.php");
    }
}
?>

<form enctype="multipart/form-data" method="post">
   <input type="text" name="headline">
   <input type="text" name="feed">
   <textarea cols="15" id="comment" name="story" placeholder="Message" rows="10"></textarea>
   <input type="text" name="type">
   <input type="file" name="image">
   <input type="submit" name="submit">
</form>

Here is my SEND function

public function send($hl,$fd,$st,$tp,$save_path,$myname)
{
  try
  {
    $stmt = $this->db->prepare("
        INSERT INTO news(headline,feed,story,type,folder,file)
        VALUES(:headline, :feed, :story, :type, :foler, :file);
    ");

    $stmt->bindparam(":headline", $hl);
    $stmt->bindparam(":feed", $fd);
    $stmt->bindparam(":story", $st);
    $stmt->bindparam(":type", $tp);
    $stmt->bindparam(":folder", $save_path);
    $stmt->bindparam(":file", $myname);

    $stmt->execute();

    return $stmt;
  }
  catch(PDOException $e)
  {
      echo $e->getMessage();
  }
}

And finally, Here is my Error.

Warning: move_uploaded_file(newimages//var/tmp/phpyctf0k) [function.move-uploaded-file]: failed to open stream: No such file or directory in /nfs/c11/h05/mnt//domains/concept/html/input.php on line 12

Warning: move_uploaded_file() [function.move-uploaded-file]: Unable to move '/var/tmp/phpyctf0K' to 'newimages//var/tmp/phpyctf0k' in /nfs/c11/h05/mnt//domains/concept/html/input.php on line 12
SQLSTATE[HY093]: Invalid parameter number: parameter was not defined

Yes I did create a new folder named newimages as well.

angelcool.net
  • 2,505
  • 1
  • 24
  • 26
KevinM1990112qwq
  • 715
  • 2
  • 10
  • 23
  • Look at the result from `$save_path.$myname` => `newimages//var/tmp/phpyctf0k`. You need to change `$myname = strtolower($_FILES['image']['tmp_name']);` **to** `$myname = strtolower($_FILES['image']['name']);` as you want the `$_FILES['image']['name']` **not** the `$_FILES['image']['tmp_name']` – Sean May 01 '16 at 03:12
  • Issue #2 - In your query you have placeholder `:foler`, but you are binding `:folder` – Sean May 01 '16 at 03:47
  • Possible duplicate of [Failed to open stream : No such file or directory](http://stackoverflow.com/questions/36577020/failed-to-open-stream-no-such-file-or-directory) – Vic Seedoubleyew May 01 '16 at 16:26

1 Answers1

0

so you're saying you've made a folder in the same relative directory of the current running php script called newimages, in which you want to upload this file. just to be absolutely clear, this is not an absolute path, and that's OK.

$save_path="newimages";

I think it needs a trailing slash, as it appears in your output, but not in your example code.

$save_path="newimages/";

on this line, you are making the whole path and filename to lowercase of the temporary file in your absolute path /var/tmp/phpyctf0k

$myname = strtolower($_FILES['image']['tmp_name']); //You are renaming the file here

so on this line, you are appending the local relative path newimages with the whole absolute path of the temp file /var/tmp/phpyctf0k, so this now indicates to the PHP interpreter that you intend to move a file to a directory that doesn't exist newimages/var/tmp/phpyctf0k

move_uploaded_file($_FILES['image']['tmp_name'], $save_path.$myname); // Move the uploaded file to the desired folder

in order to fix it, you could use something like basename

move_uploaded_file($_FILES['image']['tmp_name'], $save_path.basename($myname)); // Move the uploaded file to the desired folder

or perhaps $_FILES['image']['name'] as @Sean commented.

move_uploaded_file($_FILES['image']['tmp_name'], $save_path.strtolower($_FILES['image']['name'])); // Move the uploaded file to the desired folder
Jeff Puckett
  • 37,464
  • 17
  • 118
  • 167
  • After doing the changes I recieved this error.. SQLSTATE[HY093]: Invalid parameter number: parameter was not defined – KevinM1990112qwq May 01 '16 at 03:31
  • so did you eliminate either `$save_path` or `$myname`? – Jeff Puckett May 01 '16 at 03:33
  • Well whats weird is tried what Sean said.... It still gives me the error invalid parameter.. But it DOES save the image into the folder.. It does not save anything into the DB though. – KevinM1990112qwq May 01 '16 at 03:34
  • 1
    @kevinm1990112qwq your DB query `invaild parameter` issue is because in your query you have `:foler` but you are binding `:folder`. Basic typo error – Sean May 01 '16 at 03:51
  • @Sean One more question... When it saves the image name in my database it saves it as /var/tmp/phpdmnivo... but in my folder its named sp1.jpg – KevinM1990112qwq May 01 '16 at 03:59
  • @Sean so yea, its not saving it in the folder with the created tmp name... its saving it as the actual name. If I change it to tmp_name it shoots an error – KevinM1990112qwq May 01 '16 at 04:02