【问题标题】:Getting file names from upload script从上传脚本中获取文件名
【发布时间】:2011-11-02 23:51:45
【问题描述】:

我从 Cafewebmaster 下载了一个上传脚本。我选择了最大上传限制 3 个文件。如何获取每个单独的文件名,以便将它们放在我的 MySQL 数据库中的单独列中?

这是脚本:

<?php

/**
* Smart Image Uploader by @cafewebmaster.com
* Free for private use
* Please support us with donations or backlink
*/

$upload_image_limit = 3; // How many images you want to upload at once?
$upload_dir         = "img_upload/"; // default script location, use relative or absolute path
$enable_thumbnails  = 1 ; // set 0 to disable thumbnail creation
$max_image_size     = 1024000 ; // max image size in bytes, default 1MB

##################### THUMBNAIL CREATER FROM GIF / JPG / PNG

function make_thumbnails($updir, $img){

    $thumbnail_width    = 200;
    $thumbnail_height   = 100;
    $thumb_preword      = "thumb_";

    $arr_image_details  = GetImageSize("$updir"."$img");
    $original_width     = $arr_image_details[0];
    $original_height    = $arr_image_details[1];

    if( $original_width > $original_height ){
        $new_width  = $thumbnail_width;
        $new_height = intval($original_height*$new_width/$original_width);
    } else {
        $new_height = $thumbnail_height;
        $new_width  = intval($original_width*$new_height/$original_height);
    }

    $dest_x = intval(($thumbnail_width - $new_width) / 2);
    $dest_y = intval(($thumbnail_height - $new_height) / 2);



    if($arr_image_details[2]==1) { $imgt = "ImageGIF"; $imgcreatefrom = "ImageCreateFromGIF";  }
    if($arr_image_details[2]==2) { $imgt = "ImageJPEG"; $imgcreatefrom = "ImageCreateFromJPEG";  }
    if($arr_image_details[2]==3) { $imgt = "ImagePNG"; $imgcreatefrom = "ImageCreateFromPNG";  }


    if( $imgt ) { 
        $old_image  = $imgcreatefrom("$updir"."$img");
        $new_image  = imagecreatetruecolor($thumbnail_width, $thumbnail_height);
        imageCopyResized($new_image,$old_image,$dest_x,         
        $dest_y,0,0,$new_width,$new_height,$original_width,$original_height);
        $imgt($new_image,"$updir"."$thumb_preword"."$img");
    }

}


################################# UPLOAD IMAGES

        foreach($_FILES as $k => $v){

            $img_type = "";

            ### $htmo .= "$k => $v<hr />";  ### print_r($_FILES);

            if( !$_FILES[$k]['error'] && preg_match("#^image/#i", $_FILES[$k]['type']) && $_FILES[$k]['size'] < $max_image_size ){

                $img_type = ($_FILES[$k]['type'] == "image/jpeg") ? ".jpg" : $img_type ;
                $img_type = ($_FILES[$k]['type'] == "image/gif") ? ".gif" : $img_type ;
                $img_type = ($_FILES[$k]['type'] == "image/png") ? ".png" : $img_type ;

                $img_rname = $_FILES[$k]['name'];
                $img_path = $upload_dir.$img_rname;

                copy( $_FILES[$k]['tmp_name'], $img_path ); 
                if($enable_thumbnails) make_thumbnails($upload_dir, $img_rname);
                $feedback .= "Image and thumbnail created $img_rnam";

            }
        }


############################### HTML FORM
    while($i++ < $upload_image_limit){
        $form_img .= '<label>Image '.$i.': </label> <input type="file" name="uplimg'.$i.'"><br />';
    }

    $htmo .= '
        <p>'.$feedback.'</p>
        <form method="post" enctype="multipart/form-data">
            '.$form_img.' <br />
            <input type="submit" value="Upload Images!" style="margin-left: 50px;" />
        </form>
        ';  

    echo $htmo;

虽然我可以做类似的事情: $images1 = $_FILES['0']['name'];

但它不起作用。任何帮助表示赞赏!

谢谢

【问题讨论】:

  • 只是一个旁注......你正在做一个foreach ($_FILES as $k=&gt;$v),所以没有必要引用$_FILES。您可以使用$v
  • 我会使用来自 verot 的 class.upload

标签: php image file-upload upload


【解决方案1】:
$img_rname = $_FILES[$k]['name'];

该行应该获取文件名。但是,不要直接使用用户提供的文件名。这些数据非常不可信,恶意用户可以输入他们想要的任何文件名,包括路径数据。如果您按原样使用用户提供的名称,您的脚本将愉快地在您服务器上指定的任何文件上乱写。

同样,不要使用copy() 来移动上传的文件。效率低下 - 使用 move_uploaded_file() 代替,它明确设计用于处理上传,考虑到上传的一些安全问题 copy() 没有,并且还执行比副本快得多的移动操作(如果可能) ,尤其是大文件。

【讨论】:

  • 我可以用 move_uploaded_file() 替换 copy() 吗?我如何将名称更改为我选择的名称而不是让用户选择?谢谢
  • 是的。但您还应该检查调用的返回值,以确保文件 DID 移动。我建议将有关文件的信息存储在数据库中,并使用文件记录的 ID 号作为文件名。那是你生成的东西,用户根本无法影响。
  • 谢谢Marc,还有一个问题,如何将三个文件的名称设置为三个不同的名称?
  • 生成 3 个不同的文件名?
【解决方案2】:

您服务器上的文件名在$_FILES[...]['tmp_name'] 中。 $_FILES[...]['name'] 是用户计算机上的文件名。

【讨论】:

  • 澄清一下:tmp_name 是服务器上文件的完整路径,而name 只是文件的文件名部分(无路径)在用户的计算机上
  • 我还是一头雾水,比如我想把第一个文件中的文件名放到$image1中怎么写?
  • @2by,哪个文件名?您的服务器上存在的文件名,还是上传者计算机上的文件名?
  • 我希望将文件名放入三个不同的变量中,这样我就可以将它们放入数据库中:)
猜你喜欢
  • 2010-10-17
  • 1970-01-01
  • 1970-01-01
  • 2011-05-11
  • 2015-05-24
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-05-12
相关资源
最近更新 更多