【问题标题】:restrict image upload in uploadify在uploadify中限制图片上传
【发布时间】:2012-06-05 23:48:41
【问题描述】:

我正在使用 uploadify 上传图片。除了宽度:670px 高度:200px 的图像之外,还有什么方法可以限制用户上传。 这是我的uploadify.php 代码

<?php
require_once("includes/connection.php");

$targetFolder = '/workbench/sudeepc/photogallery/uploads' ; 

if (!empty($_FILES)) {
    $tempFile = $_FILES['Filedata']['tmp_name'];
    $targetPath = $_SERVER['DOCUMENT_ROOT'] . $targetFolder;
    $targetFile = rtrim($targetPath,'/') . '/' . $_FILES['Filedata']['name'];

    // Validate the file type
    $fileTypes = array('jpg','jpeg','gif','png'); // File extensions
    $fileParts = pathinfo($_FILES['Filedata']['name']);

    if (in_array($fileParts['extension'],$fileTypes)) {
        move_uploaded_file($tempFile,$targetFile);

 $query="INSERT INTO `photo` ( `id` , `path` , `uname` )
 VALUES (
 '','${targetFile}', 'testnn');";
 mysql_query($query,$connection);


    } else {
        echo 'Invalid file type.';
    }
}
?>

提前致谢。

【问题讨论】:

    标签: php html


    【解决方案1】:

    您无法限制实际上传 - 必须上传文件才能让服务器端逻辑测试其有效性 - 文件扩展名、尺寸等...

    有一个内置的 PHP 函数来确定图像大小 - http://php.net/manual/en/function.getimagesize.php

    getimagesize()
    

    getimagesize() 函数将确定任何给定图像的大小 文件并返回尺寸以及文件类型和 在普通 HTML IMG 标记中使用的高度/宽度文本字符串和 对应的 HTTP 内容类型。
    ...
    返回一个包含 7 个元素的数组。
    索引 0 和 1 分别包含图像的宽度和高度。

    检查文件扩展名后,您可以在图像上调用此函数并根据您的要求测试其尺寸。

    这是函数的示例输出 -

    Array ( 
      [0] => 84 // width
      [1] => 52 // height
      ... 
    )
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-08-08
      • 2013-02-28
      • 1970-01-01
      • 2016-07-21
      相关资源
      最近更新 更多