开发者

Getting file names from upload script

I downloaded an upload script from Cafewebmaster. I have choosen that the maximum is upload limit 3 files. How do I get each seperate file name, so i can put them in my MySQL database in seperate columns?

Here is the script:

<?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);



  开发者_JAVA百科  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;

I though i just could do something like: $images1 = $_FILES['0']['name'];

But it does not work. Any help appreciated!

Thanks


The name of the file on your server is in $_FILES[...]['tmp_name']. $_FILES[...]['name'] is the filename on the user's computer.


$img_rname = $_FILES[$k]['name'];

that line should be fetching the filename. However, do NOT use user-provided filenames directly. The data is highly untrustworthy, and a malicious user can enter anything they want for a filename, including path data. If you use the user-provided name as-is, your script will happily scribble over any file on your server that they've specified.

As well, don't use copy() to move the uploaded file around. It's inefficient - use move_uploaded_file() instead, which is explicitly designed to handle uploads, takes into account some security problems with uploads that copy() doesn't, and also performs a move operation (if possible) which is FAR faster than a copy, particularly on large files.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜