上传图片到服务器无法正常工作

时间:2015-02-25 15:23:38

标签: php curl file-upload upload image-uploading

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

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

我试图将这些图像上传到我的服务器,并且我在响应中成功收到但是当我检查我的目录是否图像不存在时,我认为没有上传。

在我上传图片之前,我会检查图片是否已存在于文件夹中,为此,我必须使用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>";
    }
}
?>

1 个答案:

答案 0 :(得分:2)

使用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

旁注:错误报告应仅在暂存时完成,而不是生产。