How to name uploaded image files with PHP? - php

I would like to change the code on my php uploading page so it uploaded photos with sequential numbering 1.jpg 2.jpg 3.jpg... 9999999.jpg
I want to name images in an ascending order starting with 1 and going to infinity.
I don't want any images to be over written and I think a counter can be used for this, but i'm unaware of how to code it properly.
Uploaded images are stored in two directories. 1. original file 2. thumbnail
$DestinationDirectory = 'user-uploads/'; //original file
$DestinationDirectorytn = 'user-uploads-thumbnails/'; //original file thumbnail
Currently it names the file by the time() function
// current time value, will be added as the new image name
$CurrentTime = time();
//Construct a new image name (time) for our new image.
$NewImageName = $CurrentTime.'.'.$ImageExt;

You can count the files in directory and increment the value for to name of file.
The link have example: http://www.brightcherry.co.uk/scribbles/php-count-files-in-a-directory/
After that count the files, you set the name of file: concat value with name.
Thanks
But have many other ways.

Try this:
$directory = "user-uploads/";
$filecount = count(glob($directory . "*"))+1;
//Construct a new image name (time) for our new image.
$NewImageName = $filecount.'.'.$ImageExt;

Related

How to upload images with random name rule in [codeigniter]

I'm using TinyMCE filemanager to upload images, but it always use original name whenever it stores.
Is there anyway to set the name rule (by PHP) like:
images_{auto_increment_number}
Thanks!
In codeigniter you can get the uploaded file or image name in one variable and just append that variable with another random number
$upload_data = $this->upload->data(); //Returns array of containing all of the data related to the file you uploaded.
$file_name = $upload_data['file_name'];
$append_value = 'value';
$file_name .= '_'.$append_value;
Hope this helps you out
You can use PHP uniqid() function which generates a unique ID based on the microtime (current time in microseconds). It will always create a unique file name without duplication problem.
$newimagename = uniqid() . "_image";
Hope this Helps you.
$config['encrypt_name'] = TRUE;

How to get image name automatically from destination folder

I am working on a project that resize images. My following code takes an image from upload directory, resizes it and save the output image but the problem is that I have to hard code image name.
I want to get image name automatically from upload directory. Please someone solve my problem.
<?php
include('resize_lib.php'); // resize_lib is the library that has functionality of how to resize the image
//focus on this line
$image_path = "upload/something.jpg";// hard coded image name
$resizeObj = new resize($image_path);
$resizeObj -> resizeImage(1536, 1024, 0); // width // height
$resizeObj -> saveImage("new.png", 100);
echo "done...";
?>
Try to get all images files from your uploaded directory .jpg or .png or .gif
$files = glob("upload/*.{jpg,png,gif}", GLOB_BRACE);
glob
Returns an array containing the matched files/directories, an empty
array if no file matched or FALSE on error.
You can then use foreach() loop to set your image name for $image_path. By the way you can also select only a single type of image e.g something.jpg
$files = glob('upload/*.jpg');

Encoding uploaded files name

I want to ask, if I have a web form, and people use it to upload something to my db. If 2 persons uploaded the same file with the same name, in the uploads directory, one of them will replace the other. So I need to change the name of every file uploaded to db to : filename201405051200.pdf or jpg or...
Here we have the filename per example image1 and the numbers are the date and time of the uploads. So any help. I am using the code shown as an answer in the link below:
Uploading blob files/images into Mysql
I used this code:
$path = "../uploads/".$_FILES['file']['name'];
if(move_uploaded_file($_FILES["file"]["tmp_name"], $path.'_'.time())){
...
}
but now the format type of the file is replaced by the time. So if it is img.jpg it is now img85890338jpg and wont open properly
You can use pathinfo to extract the file extension:
$fileExt = pathinfo($_FILES['file']['name'], PATHINFO_EXTENSION);
After that you can create your new file name:
if(move_uploaded_file($_FILES["file"]["tmp_name"], $path.'_'.time().date().'.'.$fileExt)) {
}

PHP AJAX - upload and crop

I found a script - Upload and Crop image on this website http://onlinewebapplication.com/2011/08/image-upload-cropping-php-jquery.html Everything works fine, but I need to add my new name for new images...
When I upload an image -> before crop I can change file name, but it works only for big images, for thumb I don't have an idea because the variables are sent by AJAX.
My version works here - link
first input is for file path, second for new filename..
I need change name for big image and thumb, but it works only for big.
How send variable "image_name" to ajax_image.php ?
Anyone ? Please help me, I need this ..Thanks
You can change the name for the big image in this line:
$actual_image_name = time().substr($txt, 5).".".$ext;
and for the small image you can pass the name as parameter by changing this:
url:"ajax_image.php?t=ajax&img="+$("#image_name").val()+"&w="...etc
to this:
url:"ajax_image.php?sname=blabla&t=ajax&img="+$("#image_name").val()+"&w="...etc
also for the small image again change in ajax_image.php this:
$new_name = "small".$session_id.".jpg"; // Thumbnail image name
to this:
$new_name = "smallqa".$_GET['sname'].".jpg"; // Thumbnail image name

PHP: Find total number of images having same name in a folder

I am creating code to upload images in a folder using PHP, & before upload an image I check that if any image with same name already exists in the folder by using below syntax:
if (file_exists('profilephoto/' . 'frame.gif')) {
}
But actually I don't want to restrict the user to upload more images with same name & obviously it is impossible to save two images with same name , but there is a way to just find total number of images with same name & upload the next image with appending total number + 1 with image name.
Now I just want to know that using PHP how could we find total number of images having same name in a folder?
<?php
$i = '';
while ( file_exists($filename = ('profilephoto/' . "frame$i.gif")) )
++$i;
echo $filename;
?>
Obviously it's subject to "race conditions," but that's probably the least of your concerns.

Categories