Php表格上传文件

时间:2016-05-26 21:12:28

标签: php html

我尝试通过php格式上传文件,但是我收到了这个错误;

[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.";
    }
}

?>

我该如何解决这个问题?

由于

更新 - 更新 - 更新 - 更新 - 更新 - 更新 - 更新 - 更新 - 更新 -

当我更改这样的代码时,它会创建/上传/ 2文件夹,但将文件上传到/ upload文件夹。

<?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);

3 个答案:

答案 0 :(得分:1)

  • 检查目录/代码是否没有拼写错误。
  • 还要确保目录具有读取+写入权限(至少为'0666',但'0755'可能会更好)。
  • 也许尝试完整路径名('path.to / public_html')。

如果此处没有任何帮助,我可以添加更多内容。请让我知道它是怎么回事!

答案 1 :(得分:1)

您需要在php.ini

中增加max_upload_filesize

答案 2 :(得分:1)

我用这个代码解决了

<?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/";