php中的文件上传器

时间:2018-05-07 11:32:43

标签: php mysql file-upload

  if (isset($_POST['submit'])) {
        $name=$_FILES['file']['name'];
        $temp=$_FILES['file']['tmp_name'];
        move_uploaded_file($temp,$_SERVER['DOCUMENT_ROOT'] ."upload/files/".$name);
        $url="http://localhost/upload/files/".$name;



        $sql = "INSERT INTO photo (NAME,link)
       VALUES ('$name','$url')";

在这段代码中,sql工作,文件名在数据库中成功更新,但我无法在目录中移动文件。

这是错误:

  

警告:   move_uploaded_file(/应用/ XAMPP / xamppfiles / htdocsupload /文件/屏幕   拍摄2018-05-07在11.17.21 AM(2).png):未能打开输入代码   herestream:没有这样的文件或目录   第45行/Applications/XAMPP/xamppfiles/htdocs/upload/upload2.php

     

“警告:move_uploaded_file():无法移动......”

请帮忙!

2 个答案:

答案 0 :(得分:0)

您的网址更改错误或尝试此操作:

    <?php
    $target_dir = "uploads/files/";

    $target_file = $target_dir . basename($_FILES["fileToUpload"]["name"]);

    $uploadOk = 1;

    $imageFileType = strtolower(pathinfo($target_file,PATHINFO_EXTENSION));

    // Check if image file is a actual image or fake image

    if(isset($_POST["submit"])) {
        $check = getimagesize($_FILES["fileToUpload"]["tmp_name"]);
    if($check !== false) {
        echo "File is an image - " . $check["mime"] . ".";
        $uploadOk = 1;
    } else {
        echo "File is not an image.";
        $uploadOk = 0;
    }
}
?>

答案 1 :(得分:0)

您可以使用以下代码上传文件

<?php
$target_dir = "uploads/";
$target_file = $target_dir . basename($_FILES["fileToUpload"]["name"]);
$uploadOk = 1;
$imageFileType = strtolower(pathinfo($target_file,PATHINFO_EXTENSION));
// Check if image file is a actual image or fake image
if(isset($_POST["submit"])) {
    $check = getimagesize($_FILES["fileToUpload"]["tmp_name"]);
    if($check !== false) {
        echo "File is an image - " . $check["mime"] . ".";
        $uploadOk = 1;
    } else {
        echo "File is not an image.";
        $uploadOk = 0;
    }
}
?>

有关详细信息,请访问https://www.w3schools.com/php/php_file_upload.asp