How do I rename an image file?

images php coding issues or problems here.

Moderators: egami, macek, gesf

Post Reply
IChaps
New php-forum User
New php-forum User
Posts: 115
Joined: Tue Mar 01, 2016 3:07 pm

Mon Nov 06, 2017 3:21 pm

Hello.

Could I please enquire, how do I rename an image file after it's been uploaded?

At present the code I'm using reads:-
[code<?php
/*
* Simple file Upload system with PHP.
* Created By Tech Stream
* Original Source at http://techstream.org/Web-Development/P ... d-With-PHP
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
*/
if(isset($_FILES['image'])){
$errors= array();
$file_name = $_FILES['image']['name'];
$file_size =$_FILES['image']['size'];
$file_tmp =$_FILES['image']['tmp_name'];
$file_type=$_FILES['image']['type'];
$file_ext=strtolower(end(explode('.',$_FILES['image']['name'])));

$expensions= array("jpeg","jpg","png");
if(in_array($file_ext,$expensions)=== false){
$errors[]="extension not allowed, please choose a JPEG or PNG file.";
}
if($file_size > 2097152){
$errors[]='File size must be excately 2 MB';
}
if(empty($errors)==true){
move_uploaded_file($file_tmp,"images/".$file_name);
chmod(/user/images/'.$file_name,777);
rename(/user/images/'.$file_name , '/user/images/0.png');
echo "Success";
}else{
print_r($errors);
}
}
?>

<form action="" method="POST" enctype="multipart/form-data">
<input type="file" name="image" />
<input type="submit"/>
</form>][/code]

I would also like to ask
If the original is a jpeg (for example), could I rename it to a number.jpg. Or if it's the original filename is a PNG, could the filename be remaned to a number.png? for example?

I'm unable to get this to work. However it is uploading ok.

Thank You.

User avatar
hyper
php-forum Fan User
php-forum Fan User
Posts: 526
Joined: Mon Feb 22, 2016 5:52 pm

Tue Nov 07, 2017 10:52 am

What happens when you upload and rename the image?

IChaps
New php-forum User
New php-forum User
Posts: 115
Joined: Tue Mar 01, 2016 3:07 pm

Tue Nov 07, 2017 2:24 pm

Hello Hyper,

Thank you for your query.
When I try and rename the image file, nothing happens.

My first thought was an incorrect path name, so I did a copy and paste and tried again, and still the image filename didn't rename.
My second thought, was the filename was read only, so I inserted a chmod, still without success.

Now, I'm a unsure what else to do.

Thanks.

User avatar
hyper
php-forum Fan User
php-forum Fan User
Posts: 526
Joined: Mon Feb 22, 2016 5:52 pm

Wed Nov 08, 2017 10:19 am

What return value are you getting and what errors are you getting?

I'm surprised that you still can't add code tags, or offer any useful information on your posts considering the length of time you have been here asking questions?

IChaps
New php-forum User
New php-forum User
Posts: 115
Joined: Tue Mar 01, 2016 3:07 pm

Wed Nov 08, 2017 2:56 pm

Hello Hyper,

First of all, leaving the code tags out, was just an error.
I'm not getting any return values or any errors reported.
It's just uploading the file and not renaming it.

And as for not explaining myself as well as you may expect. This could be due to a stoke I've suffered.

User avatar
hyper
php-forum Fan User
php-forum Fan User
Posts: 526
Joined: Mon Feb 22, 2016 5:52 pm

Wed Nov 08, 2017 4:13 pm

Have you looked at the directory using a file manager?

Post Reply