Jump to content

[SOLVED] Rename a file during upload


jaxdevil

Recommended Posts

I am trying to rename my files as they are uploaded. here is my upload and conversion code now. I just want to modify it to make the filename the same as the variable $id. Any ideas?

 

<?php
$path1= "./usedimages/$id/".$HTTP_POST_FILES['ufile']['name'][0];
copy($HTTP_POST_FILES['ufile']['tmp_name'][0], $path1);
?>
<?php
function createThumbs( $pathToImages, $pathToThumbs, $thumbWidth )
{
  // open the directory
  $dir = opendir( $pathToImages );

  // loop through it, looking for any/all JPG files:
  while (false !== ($fname = readdir( $dir ))) {
    // parse path for the extension
    $info = pathinfo($pathToImages . $fname);
    // continue only if this is a JPEG image
    if ( strtolower($info['extension']) == 'jpg' )
    {
      // load image and get image size
      $img = imagecreatefromjpeg( "{$pathToImages}{$fname}" );
      $width = imagesx( $img );
      $height = imagesy( $img );

      // calculate thumbnail size
      $new_width = $thumbWidth;
      $new_height = floor( $height * ( $thumbWidth / $width ) );

      // create a new temporary image
      $tmp_img = imagecreatetruecolor( $new_width, $new_height );

      // copy and resize old image into new image
      imagecopyresized( $tmp_img, $img, 0, 0, 0, 0, $new_width, $new_height, $width, $height );

      // save thumbnail into a file
      imagejpeg( $tmp_img,"{$pathToThumbs}{$fname}") or die ("Could not create image from JPEG");
    }
  }
  // close the directory
  closedir( $dir );
}
// call createThumb function and pass to it as parameters the path
// to the directory that contains images, the path to the directory
// in which thumbnails will be placed and the thumbnail's width.
// We are assuming that the path will be a relative path working
// both in the filesystem, and through the web for links
createThumbs("./usedimages/$id/","./usedimages/$id/thumbs/","150");
?>

Link to comment
https://forums.phpfreaks.com/topic/105669-solved-rename-a-file-during-upload/
Share on other sites

I really wish I could actually get help here, most of the time lately I have to keep figuring these out for myself. Here is the fix:

 

<?php
$path1= "./usedimages/$id/".$HTTP_POST_FILES['ufile']['name'][0];
copy($HTTP_POST_FILES['ufile']['tmp_name'][0], $path1);
chmod("$path1", 0755);
$orig_filename = "$path1";
$new_filename = "./usedimages/$id/$id.jpg";
$status = rename($orig_filename, $new_filename) or exit("Could not add the item");
echo "Item added successfully!";
?>
<?php
function createThumbs( $pathToImages, $pathToThumbs, $thumbWidth )
{
  // open the directory
  $dir = opendir( $pathToImages );

  // loop through it, looking for any/all JPG files:
  while (false !== ($fname = readdir( $dir ))) {
    // parse path for the extension
    $info = pathinfo($pathToImages . $fname);
    // continue only if this is a JPEG image
    if ( strtolower($info['extension']) == 'jpg' )
    {
      // load image and get image size
      $img = imagecreatefromjpeg( "{$pathToImages}{$fname}" );
      $width = imagesx( $img );
      $height = imagesy( $img );

      // calculate thumbnail size
      $new_width = $thumbWidth;
      $new_height = floor( $height * ( $thumbWidth / $width ) );

      // create a new temporary image
      $tmp_img = imagecreatetruecolor( $new_width, $new_height );

      // copy and resize old image into new image
      imagecopyresized( $tmp_img, $img, 0, 0, 0, 0, $new_width, $new_height, $width, $height );

      // save thumbnail into a file
      imagejpeg( $tmp_img,"{$pathToThumbs}{$fname}") or die ("Could not create image from JPEG");
    }
  }
  // close the directory
  closedir( $dir );
}
// call createThumb function and pass to it as parameters the path
// to the directory that contains images, the path to the directory
// in which thumbnails will be placed and the thumbnail's width.
// We are assuming that the path will be a relative path working
// both in the filesystem, and through the web for links
createThumbs("./usedimages/$id/","./usedimages/$id/thumbs/","150");
?>

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.