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);
6
  • 1
    if (!file_exists("../uploads/ $orderid ")) { remove the spaces and in mkdir("../uploads/ $orderid ", 0700); Commented May 26, 2016 at 21:24
  • @RiggsFolly not fixed :( i get same error Commented May 26, 2016 at 21:27
  • Try removing the following from $target_dir /home/tefotv Commented May 26, 2016 at 22:08
  • @Brett not solved. i get same error Commented May 26, 2016 at 22:13
  • 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" Commented May 26, 2016 at 22:30

3 Answers 3

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!

Sign up to request clarification or add additional context in comments.

6 Comments

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"
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
That's simple. Just use header("Location: [yourpage.php]"); and it should be fine. :)
Thank you for your really fast answer :)
If all problems are solved, you should mark answer as accepted for future visitors. You will also get +2 reputation. :)
|
1

You need to increase the max_upload_filesize in php.ini

3 Comments

Could you maybe provide a code line how to increase the max upload size and suggest a proper size?
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 I try with 10KB File :)
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/";

1 Comment

I delete 2 space at the end of tarket_dir and changed permission to mkdir("../uploads/$orderid", 0777);

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.