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

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;

Related

how to save the name of a file in mysql using plupload

I wonder how I do to save the name of an image in mysql with plupload?
In file upload.php have many variables and I do not know which one stores the filename, already tried everything and could not
I would like to save the image name that plupload creates, not the original name
can someone help me?
thank you
Get the file name like this.
$filename = $_FILES["file"]["name"];
and pass $filename into insert statement.
Try this plugin for plupload to upload file from github
You can directly used a $fileName variable
here give you a example, like
$sSQL = "INSERT INTO eletz_multiimage (image_name) VALUES ('$fileName')";
$result = mysql_query($sSQL);

How to name uploaded image files with 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;

How can i avoid same image name at the point of upload to filesystem

I have a form whereby Users upload pictures as part of their registration process. But my issue is that, users that save the names of their image with the same name might cause some issues when they view the profile pictures. How can i make the picture name unique at the point of uploading to the filesystem..
Thanks.
I would suggest using one of the following schemes:
make the filename a function of the username/userid
make the filename a random value
make the filename a function of the file content (e.g. an MD5 checksum)
If this helps
$t = uniqid();
$filename='user-'.$t;
echo $filename; // user-1335601088 (always a different name)

I want to create a file that been given name by the user and save some image in it ? in php

i want to create a file.The name of the file is given by the user.So i take the textfield value to $name and how can i save a file with that name? i know how to save with a static name like
$myfile="ads.txt"
so my question is.is there a way that i can create a file with the name $name and save some stuff in it like image.
$file = fopen($name, 'wb'); // for the love of God and all that is holy, please validate $name
fwrite($file, $data);
fclose($file);
You can't save images in a txt file.
What you must do is save the actual image to your server (uploaded via a file field) and save to a database the location and name of the image, along with the user id, so you know who uploaded it.
There is quite a large amount of code involved, so i would suggest googling for "php file upload tutorial", plenty of examples out there.

is it possible to change the name of a file after it's been uploaded

Is there any way for a php script to choose the name for a file after it's been uploaded by the user using an HTML form? I am wanting to allow users to upload an avatar for their account and would like it named with their userid instead of whatever the name of it is on their computer. I'm using a basic HTML upload form which only allows jpegs and png files with a 10MB file limit, similar to the file upload code give on http://www.w3schools.com/php/php_file_upload.asp Any help you can give will be greatly appreciated.
Put the desired filename in the second argument of move_uploaded_file().
You can specify the filename when using move_uploaded_file(), otherwise you can rename() the file.
$userid = 5; // say you fetch it from database
$ext = explode("\/",$_FILES["file"]["type"]); //extract the file extension
move_uploaded_file($_FILES["file"]["tmp_name"], "upload/".$userid.$ext[1]);
UPDATE:
I think you don't need to extract file extension.
move_uploaded_file($_FILES["file"]["tmp_name"], "upload/".$userid);

Categories