2014-07-19 61 views
0

我已经使用本教程构建图片上传器。图片上传,无法识别文件扩展名

http://www.codeforest.net/upload-crop-and-resize-images-with-php

这使用该库

https://gist.github.com/philBrown/880506/download#

然而,当过我通过表单传递一个文件给它,它总是失败的最后一个IF语句(请上传图片)

下面是代码:

<?php 
require_once(dirname(__FILE__).'/../config.php'); 
require_once(dirname(__FILE__).'/ImageManipulator.php'); 

if ($_FILES['fileToUpload']['error'] > 0) { 
    echo "Error: " . $_FILES['fileToUpload']['error'] . "<br />"; 
} else { 
    // array of valid extensions 
    $validExtensions = array('.jpg', '.jpeg', '.gif', '.png', '.JPG', '.JPEG', '.PNG', '.GIF', '.bmp'); 
    // get extension of the uploaded file 
    $fileExtension = strrchr($_FILES['fileToUpload']['name'], "."); 
    // check if file Extension is on the list of allowed ones 
    if (in_array($fileExtension, $validExtensions)) { 
     $newNamePrefix = time() . '_'; 
     $manipulator = new ImageManipulator($_FILES['fileToUpload']['tmp_name']); 
     $width = $manipulator->getWidth(); 
     $height = $manipulator->getHeight(); 
     $centreX = round($width/2); 
     $centreY = round($height/2); 
     // our dimensions will be 80by80 
     $x1 = $centreX - 40; // 80 
     $y1 = $centreY - 40; // 80 

     $x2 = $centreX + 40; // 80 
     $y2 = $centreY + 40; // 80 

     // center cropping to 80by80 
     $newImage = $manipulator->crop($x1, $y1, $x2, $y2); 
     // saving file to uploads folder 
     $manipulator->save('/images/avatars/uploaded/' . $newNamePrefix . $_FILES['fileToUpload']['name']); 
     echo 'Done ...'; 
    } else { 
     echo 'You must upload an image...'; 
    } 
} 
?> 

这里是表格。

<h4>Change Avatar</h4> 
    <form enctype="multipart/form-data" method="post" action="/lib/uploadimage.php"><table class="table table-striped"> 
    <tr><td>Please choose a file:</td><td><input type="file" name="fileToUpload" id="fileToUpload" /> 
    <tr><td colspan="2"><input type="submit" value="Upload"></td></tr> 
    </table></form> 

我打算一旦我有这个工作,将URL插入数据库。

我假设它在事物的上传方面失败,但是apache日志不会给我任何错误,并且该文件夹也有757个权限。

任何想法为什么这是失败?

编辑:通过Apache的错误是

log:[Sat Jul 19 20:48:51 2014] [error] [client 82.46.57.58] PHP Notice: Undefined index: fileToUpload in /var/SITE/lib/uploadimage.php on line 5, referer: SITE/index.php 

编辑:进一步挖掘,这似乎是一个权限问题

SITE:[Sat Jul 19 21:31:58 2014] [error] [client 82.46.57.58] PHP 2. ImageManipulator->save() /SITE/lib/uploadimage.php:30, referer: SITE/index.php 
SITE:[Sat Jul 19 21:31:58 2014] [error] [client 82.46.57.58] PHP 3. mkdir() /SITE/lib/ImageManipulator.php:222, referer: SITE/index.php 
SITE:[Sat Jul 19 21:31:58 2014] [error] [client 82.46.57.58] PHP Fatal error: Uncaught exception 'RuntimeException' with message 'Error creating directory /images/avatars/uploaded' in /SITE/lib/ImageManipulator.php:223\nStack trace:\n#0 /SITE/lib/uploadimage.php(30): ImageManipulator->save('/images/avatars...')\n#1 {main}\n thrown in /SITE/lib/ImageManipulator.php on line 223, referer: SITE/index.php 

回答

0

它是基于许可,并在图像目录中的一个小错误。

它应该是../images,并且文件夹需要由www-data拥有。