繁体   English   中英

Php表格上传文件

[英]Php Form Uploading File

我尝试通过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.";
    }
}

?>

我怎样才能解决这个问题?

谢谢

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

当我更改这样的代码时,它会创建/ uploads / 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);
  • 检查目录/代码是否没有拼写错误。
  • 还要确保该目录具有读写权限(至少'0666'但'0755'可能更好)。
  • 也许尝试完整路径名('path.to / public_html')。

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

你需要在php.ini中增加max_upload_filesize

我用这个代码解决了

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM