php中的mysql文件上传程序

p4tfgftt  于 2021-06-25  发布在  Mysql
关注(0)|答案(2)|浏览(324)
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 \u uploaded \u file(/applications/xampp/xamppfiles/htdocsupload/files/screen shot 2018-05-07 at 11.17.21 am(2).png):无法打开此处的输入代码流:第45行的/applications/xampp/xamppfiles/htdocs/upload/upload2.php中没有此类文件或目录
“警告:移动\u上载的\u文件():无法移动…”
救命啊!

k4ymrczo

k4ymrczo1#

您有一个错误的url请更改您的url或尝试以下操作:

<?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;
    }
}
?>
ycggw6v2

ycggw6v22#

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

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

相关问题