上传图片到服务器不工作

Upload image to server not working

在开始之前,我想澄清一下,我对此进行了广泛的研究,但 none 的解决方案有效。

我一直在创建一个应用程序,允许用户上传您的图片以创建活动。

我尝试将此图片上传到我的服务器,我在响应中成功收到,但是当我检查我的目录是否没有图片时,我认为没有上传。

在上传图像之前,我检查图像是否已存在于文件夹中,为此我必须使用 curl 创建一个函数来验证这一点,因为函数 file_exists 没有工作。

我不知道我还需要做些什么才能得到这个。 P.S:我正在使用 backbone.js 构建我的应用程序。

这里是我的代码: html:

<form id="uploadimage" action="" method="post" enctype="multipart/form-data">
                <input type="file" name="file" id="file">
            </form>

view.js

events: {
        'click #guardar' : 'guardarEvento',
        'click #guardarImagem' : 'guardarImagem',
        'change input[type=file]' : 'uploadFile'    
    },

guardarImagem: function(){

    var fileInput = document.getElementById('uploadimage');
    console.log(new FormData(fileInput));
    $.ajax({
        url: "js/views/ajax_php_file.php", // Url to which the request is send
        type: "POST",             // Type of request to be send, called as method
        data: new FormData(fileInput), // Data sent to server, a set of key/value pairs (i.e. form fields and values)
        contentType: false,       // The content type used when sending data to the server.
        cache: false,             // To unable request pages to be cached
        processData:false,        // To send DOMDocument or non processed data file it is set to false
        success: function(data){   // A function to be called if request succeeds
            console.log(data);
        }
    }); 
},

ajax_php_file.php

<?php
if(isset($_FILES["file"]["type"])){
    $validextensions = array("jpeg", "jpg", "png");
    $temporary = explode(".", $_FILES["file"]["name"]);
    $file_extension = end($temporary);

    if ((($_FILES["file"]["type"] == "image/png") || ($_FILES["file"]["type"] == "image/jpg") || ($_FILES["file"]["type"] == "image/jpeg")
    ) && ($_FILES["file"]["size"] < 100000)//Approx. 100kb files can be uploaded.
    && in_array($file_extension, $validextensions)) {
        if ($_FILES["file"]["error"] > 0){
            echo "Return Code: " . $_FILES["file"]["error"] . "<br/><br/>";
        }else{
            if (file_exists("http://www.***/webtools/ondequemvaiver/agora/img/" . $_FILES["file"]["name"])){
                echo $_FILES["file"]["name"] . " <span id='invalid'><b>already exists.</b></span> ";
            }else{
                $sourcePath = $_FILES['file']['tmp_name']; // Storing source path of the file in a variable
                $targetPath = "http://www.***/webtools/ondequemvaiver/agora/img/".$_FILES['file']['name']; // Target path where file is to be stored

                move_uploaded_file($sourcePath,$targetPath) ; // Moving Uploaded file


                echo "<span id='success'>Image Uploaded Successfully...!!</span><br/>";
                echo "<br/><b>File Name:</b> " . $_FILES["file"]["name"] . "<br>";
                echo "<b>Type:</b> " . $_FILES["file"]["type"] . "<br>";
                echo "<b>Size:</b> " . ($_FILES["file"]["size"] / 1024) . " kB<br>";
                echo "<b>Temp file:</b> " . $_FILES["file"]["tmp_name"] . "<br>";
            }
        }
    }else{
        echo "<span id='invalid'>***Invalid file Size or Type***<span>";
    }
}
?>

使用 move_uploaded_file() 而不是 http:// 调用时必须使用系统路径。

即:

$targetPath = "/var/user/httpdocs/webtools/ondequemvaiver/agora/img/...

$targetPath = "webtools/ondequemvaiver/agora/img/...

取决于脚本执行的位置。

您还需要确保文件夹具有适当的权限才能写入。

error reporting 添加到您的文件的顶部,这将有助于查找错误。

<?php 
error_reporting(E_ALL);
ini_set('display_errors', 1);

// rest of your code

旁注:错误报告只应在试运行中进行,绝不能在生产中进行。