dongyan1974 2014-07-19 19:53
浏览 48

图像上传,无法识别文件扩展名

I have used this tutorial to build an image uploader.

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

This uses the library

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

However, when ever I pass a file to it via the form, it always fails on the last IF statement (please upload an image)

Below is the code :

<?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...';
    }
}
?> 

And here is the form.

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

I plan on once I have this working, inserting the URL into a database.

I'm assuming it fails on the upload side of things, however the apache logs don't give me any errors and also the folder has 757 permissions.

Any idea why this is failing?

EDIT: The error via apache is

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

EDIT: Digging deeper, it seems to be a permission issue

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
Stack trace:
#0 /SITE/lib/uploadimage.php(30): ImageManipulator->save('/images/avatars...')
#1 {main}
  thrown in /SITE/lib/ImageManipulator.php on line 223, referer: SITE/index.php
  • 写回答

1条回答 默认 最新

  • doutuobao9736 2014-07-19 21:40
    关注

    It was permission based and a small error in the image directory.

    It should of been ../images, and folder needed to be owned by www-data.

    评论

报告相同问题?

悬赏问题

  • ¥100 set_link_state
  • ¥15 虚幻5 UE美术毛发渲染
  • ¥15 CVRP 图论 物流运输优化
  • ¥15 Tableau online 嵌入ppt失败
  • ¥100 支付宝网页转账系统不识别账号
  • ¥15 基于单片机的靶位控制系统
  • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度