简体   繁体   English

重命名图像文件(如果服务器上已经存在PHP)

[英]Rename an image file if already exists on the server PHP

I'd like to add suffix to the filename if the file already exists on the server. 如果文件已经存在于服务器上,我想在文件名中添加后缀。

For example: if the file image.jpg exists on the server the new file to be renamed to image_1.jpg, image_2.jpg etc. 例如:如果服务器上存在文件image.jpg,则将新文件重命名为image_1.jpg,image_2.jpg等。

Any help would be much appreciated! 任何帮助将非常感激!

$max_file_size = 1024*100000; // 100000kb
    $valid_exts = array('jpeg', 'jpg', 'png', 'gif');
    // thumbnail sizes
    $sizes = array(250 => 150);

    if (isset($_FILES["$fileime"])) {
      if( $_FILES["$fileime"]['size'] < $max_file_size ){
        // get file extension
        $ext = strtolower(pathinfo($_FILES["$fileime"]['name'], PATHINFO_EXTENSION));
        if (in_array($ext, $valid_exts)) {
          /* resize image */
          foreach ($sizes as $width => $height) {
            /* Get original image x y*/
                  list($w, $h) = getimagesize($_FILES["$fileime"]["tmp_name"]);
                  /* calculate new image size with ratio */
                  $ratio = max($width/$w, $height/$h);
                  $h = ceil($height / $ratio);
                  $x = ($w - $width / $ratio) / 2;
                  $w = ceil($width / $ratio);
                  /* new file name */
                  $path = '../thumbs/'.$_FILES["$fileime"]['name'];
                  /* read binary data from image file */
                  $imgString = file_get_contents($_FILES["$fileime"]['tmp_name']);
                  /* create image from string */
                  $image = imagecreatefromstring($imgString);
                  $tmp = imagecreatetruecolor($width, $height);
                  imagecopyresampled($tmp, $image,
                    0, 0,
                    $x, 0,
                    $width, $height,
                    $w, $h);

Why not just add the timestamp at the end of your file name? 为什么不只在文件名的末尾添加时间戳?

$max_file_size = 1024*100000; // 100000kb
    $valid_exts = array('jpeg', 'jpg', 'png', 'gif');
    // thumbnail sizes
    $sizes = array(250 => 150);

    if (isset($_FILES["$fileime"])) {
      if( $_FILES["$fileime"]['size'] < $max_file_size ){
        // get file extension
        $ext = strtolower(pathinfo($_FILES["$fileime"]['name'], PATHINFO_EXTENSION));
        if (in_array($ext, $valid_exts)) {
          /* resize image */
          foreach ($sizes as $width => $height) {
            /* Get original image x y*/
                  list($w, $h) = getimagesize($_FILES["$fileime"]["tmp_name"]);
                  /* calculate new image size with ratio */
                  $ratio = max($width/$w, $height/$h);
                  $h = ceil($height / $ratio);
                  $x = ($w - $width / $ratio) / 2;
                  $w = ceil($width / $ratio);
                  /* new file name */
                  $filename_array = explode('.', $_FILES["$fileime"]['name']);
                  $extension = array_pop($filename_array);
                  $new_name = implode('.',$filename_array).'_'.time().'.'.$extension;
                  $path = '../thumbs/'.$new_name;
                  /* read binary data from image file */
                  $imgString = file_get_contents($_FILES["$fileime"]['tmp_name']);
                  /* create image from string */
                  $image = imagecreatefromstring($imgString);
                  $tmp = imagecreatetruecolor($width, $height);
                  imagecopyresampled($tmp, $image,
                    0, 0,
                    $x, 0,
                    $width, $height,
                    $w, $h);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM