1

I try to upload file via php form but i get this error;

[26-May-2016 17:07:55 America/Detroit] PHP Warning:  move_uploaded_file(../uploads/2/slider2.jpg): failed to open stream: No such file or directory in /home/tefotv/public_html/6/php/fileupload.php on line 38
[26-May-2016 17:07:55 America/Detroit] PHP Warning:  move_uploaded_file(): Unable to move '/tmp/php6SxeW0' to '../uploads/2/slider2.jpg' in /home/tefotv/public_html/6/php/fileupload.php on line 38

fileupload.php

<?php
include('../connect.php');
session_start(); 
header("refresh:1;url=../orders.php");
$orderid = $_SESSION['orderid'];
$sender = $_SESSION['id'];

if (!file_exists("../uploads/ $orderid ")) {
    mkdir("../uploads/ $orderid ", 0700);
}

$target_dir = "/home/tefotv/public_html/6/uploads/$orderid/";
$target_file = $target_dir . basename($_FILES["fileToUpload"]["name"]);
$uploadOk = 1;
$imageFileType = pathinfo($target_file,PATHINFO_EXTENSION);

// Check if file already exists
if (file_exists($target_file)) {
    echo "Sorry, file already exists.";
    $uploadOk = 0;
}
// Check file size
if ($_FILES["fileToUpload"]["size"] > 500000) {
    echo "Sorry, your file is too large.";
    $uploadOk = 0;
}
// Allow certain file formats
if($imageFileType != "jpg" && $imageFileType != "png" && $imageFileType != "jpeg"
&& $imageFileType != "gif" && $imageFileType != "pdf" ) {
    echo "Sorry, only JPG, JPEG, PNG & GIF & PDF files are allowed.";
    $uploadOk = 0;
}
// Check if $uploadOk is set to 0 by an error
if ($uploadOk == 0) {
    echo "Sorry, your file was not uploaded.";
// if everything is ok, try to upload file
} else {
    if (move_uploaded_file($_FILES["fileToUpload"]["tmp_name"], $target_file)) {
        echo "The file ". basename( $_FILES["fileToUpload"]["name"]). " has been uploaded.";
        //Buradan itibaren
        $filename = $_FILES["fileToUpload"]["name"];
        $query = $db->prepare("INSERT INTO orderfile SET orderid = ?, userid = ?, patch = ? "); 
        $insert = $query->execute(array( $orderid, $sender, $filename ));
        echo "UPDATED";

    } else {
        echo "Sorry, there was an error uploading your file.";
    }
}

?>

How can i fix this?

Thanks

Update- Update- Update- Update- Update- Update- Update- Update- Update-

When i changed code like this, it creates /uploads/2 folder but upload file to /upload folder.

<?php
include('../connect.php');
session_start(); 
header("refresh:1;url=../orders.php");
        $orderid = $_SESSION['orderid'];
        $sender = $_SESSION['id'];

if (!file_exists("../uploads/$orderid ")) {
    mkdir("../uploads/$orderid ", 0755);
}

$target_dir = "../uploads/$orderid";
$target_file = $target_dir . basename($_FILES["fileToUpload"]["name"]);
$uploadOk = 1;
$imageFileType = pathinfo($target_file,PATHINFO_EXTENSION);
Can1
  • 89
  • 1
  • 13

3 Answers3

1
  • Check if directory/code has no spelling mistakes.
  • Also make sure the directory has read + write permissions (at least ´0666´ but ´0755´ might work better).
  • Perhaps try full path name (´path.to/public_html´).

I can add more if nothing from here helps. Please let me know how it goes!

Gynteniuxas
  • 7,035
  • 18
  • 38
  • 54
  • When i add / at the end of this code, it not works. When i removed /, it upload file one upper directory. $target_dir = "../uploads/$orderid" – Can1 May 26 '16 at 22:36
  • Ok i fixed problem but after upload, page not show the new file. I need to refresh page for see new file. How can i fix this? Could you help me – Can1 May 27 '16 at 20:36
  • That's simple. Just use `header("Location: [yourpage.php]");` and it should be fine. :) – Gynteniuxas May 27 '16 at 20:37
  • Thank you for your really fast answer :) – Can1 May 27 '16 at 20:59
  • If all problems are solved, you should mark answer as accepted for future visitors. You will also get +2 reputation. :) – Gynteniuxas May 27 '16 at 21:07
  • There is troubleshooting checklist for this error here : http://stackoverflow.com/questions/36577020/failed-to-open-stream-no-such-file-or-directory – Vic Seedoubleyew May 27 '16 at 21:13
1

You need to increase the max_upload_filesize in php.ini

Arno
  • 11
  • 1
  • Could you maybe provide a code line how to increase the max upload size and suggest a proper size? – Martin Gottweis May 26 '16 at 22:10
  • upload_max_filesize = 64M post_max_size = 64M, this happen when you re-install apache/php. You can try to upload a smaal file first. In case of succes these settings are the problem. – Arno May 26 '16 at 22:22
  • @Arno I try with 10KB File :) – Can1 May 26 '16 at 22:28
1

I solved with this codes

<?php
include('../connect.php');
session_start(); 
header("refresh:1;url=../orders.php");
        $orderid = $_SESSION['orderid'];
        $sender = $_SESSION['id'];

if (!file_exists("../uploads/$orderid")) {
    mkdir("../uploads/$orderid", 0777);
}

$target_dir = "/home/tefotv/public_html/6/uploads/$orderid/";
Can1
  • 89
  • 1
  • 13
  • I delete 2 space at the end of tarket_dir and changed permission to mkdir("../uploads/$orderid", 0777); – Can1 May 26 '16 at 23:12