Jump to content

Recommended Posts

Hi there,

 

I am using this code to rename uploaded images:

 

$target_path = "images/"; 
$target_path = $target_path . basename( $_FILES['Image1']['name']); 
$_FILES['Image1']['tmp_name'];  
$target_path = "images/"; 

$filename = basename( $_FILES['Image1']['name']);

$filename = time() . '_' . $filename;

$target_path .= $filename;

if(move_uploaded_file($_FILES['Image1']['tmp_name'], $target_path)) {
echo "Image was uploaded successfully!";
} else{     
print"Image could not be uploaded!";
}

 

So, if the image is called..

ME.jpg  > Becomes > 1285035030_ME.jpg

ROOM.gif  > Becomes > 1186085030_ROOM.gif

 

 

But i want all the images to be called UPLOADED AND to keep the extension of the image only...

ROOM.jpg  > Becomes > 1285535030_UPLOADED.jpg

ROOM.gif  > Becomes > 1185036030_UPLOADED.gif

 

Can you show me how to change this code?

Link to comment
https://forums.phpfreaks.com/topic/69449-solved-rename-uploaded-images/
Share on other sites

First of all the beginning of your code is pretty weird:

 

<?php
$target_path = "images/"; 
$target_path = $target_path . basename( $_FILES['Image1']['name']); 
$_FILES['Image1']['tmp_name'];  
$target_path = "images/"; 
?>

 

You overwrite your $target_path variable twice... why?

 

 

Secondly, this is the part you need to change:

 

<?php

$filename = basename( $_FILES['Image1']['name']);

$filename = time() . '_' . $filename;

$target_path .= $filename;

?>

 

Just set $filename = 'UPLOADED';

 

<?php

$filename = 'UPLOADED';

$filename = time() . '_' . $filename;

$target_path .= $filename;

?>

<?php

//Since you redeclare $taget_path later this does nothing
$target_path = "images/";

//Since you redeclare $taget_path later this does nothing too
$target_path = $target_path . basename( $_FILES['Image1']['name']);

//This does nothing...
$_FILES['Image1']['tmp_name'];

//Here you "reset" $target_path to "images/" - this is why the previous lines does nothing
$target_path = "images/"; 

//Here you assing the original filename to $filename
$filename = basename( $_FILES['Image1']['name']);

//Here you put a timestamp and _ (underscore) in front of the original filename
$filename = time() . '_' . $filename;

//Here you combine $target_path ("images/") with $filename (1234567890_orgfilename.ext) and get "images/1234567890_orgfilename.ext";
$target_path .= $filename;

if(move_uploaded_file($_FILES['Image1']['tmp_name'], $target_path)) {
    echo "Image was uploaded successfully!";
} else{     
    print "Image could not be uploaded!";
}

?>

wow, iam using your code now

 

I removed all the bits which do nothing, and it works fine

 

Last thing,You showd me how to rename them to UPLOADED

 

do you know how i rename them to UPLOADED.File extension

 

Any idea how this can be done

 

$filename = 'UPLOADED'; < this removes the extension of the image

All you need to do is manipulate $target_path until it has the proper value.

 

First of all you want all your images to go in the a sub directory called "images", this is why you do this:

 

<?php
$target_path = 'images/';
?>

 

Now if you do not want the images to go in any more sub directories you need to append the filename to the end of the $target_path variable so it becomes "images/filename.ext". This means you need to find out what you want to call your uploaded files. It seems to be something with a timestamp, followed by a _ (underscore), followed by 'UPLOADED' followed by the original extension. This is almost accomplished by this:

 

 

<?php
$target_path = 'images/';
$target_path .= time() . '_UPLOADED';
?>

 

Now your $target_path looks like this: 'images/1234567890_UPLOADED'

 

What is missing? The extension of course! So what to do? Get the extension!

 

http://www.scriptplayground.com/tutorials/php/Get-File-Extension/

 

Once you have a variable called $ext ($str in the example I linked to) you only need to append it to the rest of $target_path:

 

<?php
$target_path = 'images/';
$target_path .= time() . '_UPLOADED';
$target_path .= $ext;

//Remember that $ext might not contain the . (dot) but only the extension, for example jpg not .jpg. This means you need to add the dot yourself.
$target_path .= $ext;

//Can be written as
$target_path = 'images/' . $time . '_UPLOADED' . $ext;


?>

 

 

EDIT: Changed "//Remember that $ext might now contain the . (dot)" to "//Remember that $ext might not contain the . (dot)"

You should write a book  ;)

 

I used it like that, though, it doesnt work for some reason, is that a mistake on my code

 

<?php


function getExtension($Image1) {
  $pos = strrpos($Image1, '.');
  if(!$pos) {
    return 'Unknown Filetype';
  }
  $ext = substr($Image1, $pos, strlen($Image1));
  return $ext;
}


$target_path = 'images/' . $time . '_UPLOADED' . $ext;

if(move_uploaded_file($_FILES['Image1']['tmp_name'], $target_path)) {
echo "Image was uploaded successfully!";
} else{     
print "Image could not be uploaded!";
}




?> 

Woaah! There was no reason to mark the thread/topic as SOLVED. All I was trying to do was to make you understand the code you were using... There is no reason why you should not end up with something working.

 

The reason why it's not working is because you defined the "get extension code" as a function but you do not call the function and therefore it does nothing. To correct the code you posted:

 

<?php

//You can call the function argument anything, it does not have anything to do with your current script and $image1... call it $filename since it can be used to get the ext form any filename...
function getExtension($filename) {
  $pos = strrpos($filename, '.');
  if(!$pos) {
    return 'Unknown Filetype';
  }
  $ext = substr($filename, $pos, strlen($filename));
  return $ext;
}

//You need to call the function when the extension is needed
$target_path = 'images/' . $time . '_UPLOADED' . getExtension($_FILES['Image1']['tmp_name']);

if(move_uploaded_file($_FILES['Image1']['tmp_name'], $target_path)) {
echo "Image was uploaded successfully!";
} else{     
print "Image could not be uploaded!";
}

?>

 

 

But there is no real reason for creating a function... you only need the code once. Do something like this:

 

<?php

//Assing a more nifty name to $_FILES['Image1']['tmp_name']
$image = $_FILES['Image1']['tmp_name'];


//Define the upload dir:
$upload_dir = 'images/';

//Get the files extension
$pos = strrpos($image, '.');
$ext = substr($image, $pos, strlen($image));


//Create the final filename
$output_filename = time() . '_UPLOADED' . $ext;


//Construct the target path
$target_path = $upload_dir . $output_filename;



if(move_uploaded_file($image, $target_path)) {
    echo "Image was uploaded successfully!";
} else{     
    print "Image could not be uploaded!";
}

?> 

 

 

 

But I must recommend you to get your hands on some PHP books or search the internet for some basic PHP tutorials. You are missing alot of fundamental stuff.

 

If it does not work post again.

He sure could. There are lots of ways to obtain the "extension" of a filename:

 

#1

<?php
$pos = strrpos($image, '.');
$ext = substr($image, $pos, strlen($image));
?>

 

 

#2

<?php
$ext = explode($image);

//$ext[1] will hold the extension
?>

 

Just to mention a few. Some of them might be faster than others but I don't know which is fastest. I've always used the position method...

 

#3

<?php
$path_info = pathinfo($filename);

//$path_info['extension'] will hold the extension
?>

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.