簡體   English   中英

重命名圖像文件(如果服務器上已經存在PHP)

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

如果文件已經存在於服務器上,我想在文件名中添加后綴。

例如:如果服務器上存在文件image.jpg,則將新文件重命名為image_1.jpg,image_2.jpg等。

任何幫助將非常感激!

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

為什么不只在文件名的末尾添加時間戳?

$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