how to put date time image name with my code? - php

how to put date time in my image upload name code?
$allowed_types=array('image/jpg','image/png');
$image_name=$_FILES['thumnail']['name'];
$image_name=$image_name;
$tmp_name=$_FILES['thumnail']['tmp_name'];
move_uploaded_file($tmp_name,"upload/".$image_name);

Try this.
$allowed_types=array('image/jpg','image/png');
$image_name=$_FILES['thumnail']['name'];
$array = explode('.', $image_name);
$fileName=$array[0];
$fileExt=$array[1];
$image_name=$fileName."_".time().".".$fileExt;
$tmp_name=$_FILES['thumnail']['tmp_name'];
move_uploaded_file($tmp_name,"upload/".$image_name);

Related

PHP $filename with text

Quick question (hopefully)
I am saving a form as a pdf using php and would like to save the $filename as something like:
/New_Form_[user].pdf
(where [user] is $POST['forename']+$POST['surname'])
Is this possible? if so how?
my current code:
$filename = "HTC_619 New User.pdf";
The "New User" bit i want auto populated from Firstname and Surname from the form.
so:
$filename = "HTC_619_$POST['Forename']_$POST['Surname']
Thanks
Phil
yes you can do it like
header("Content-Disposition: attachment; filename=\"$filename\"")
$filename = "HTC_619". $username."_".$surname.".pdf";
pass the post first name and last name in $filename variable
$name=$_POST['forename']+$_POST['surname'];
$filename="htc_619"."_".$name.".pdf";

how to give unique name to file in php in loop

I m just renaming file in directory....
...bt my files got duplicate...
due to duplicate .... some file get deleted...
I have an issue in my script...
bt i m unable to solve this..
my script---
for($i=2;$i<=count($worksheet);$i++)
{
$directory = $_SERVER['DOCUMENT_ROOT'].'/mesleep/uploaded_files/test/';
$sku = (!isset($worksheet[$i][1])) ? '' : addslashes(trim($worksheet[$i][1]));
$image_1 = (!isset($worksheet[$i][2])) ? '' : htmlentities(trim($worksheet[$i][2]));
rename($directory.$image_1,$directory.$sku.'_'.$i);
$image_2 = (!isset($worksheet[$i][3])) ? '' : htmlentities(trim($worksheet[$i][3]));
rename($directory.$image_2,$directory.$sku.'_'.$i);
}
how can i make my image name unique...
Try this http://php.net/manual/en/function.uniqid.php
It will help give unique strings.
$uniqueString = uniqid(random(),true);
There are many ways to do so :
You can add time() before name.
Example:
$imageName="XYZ.png";
$uniqueImageName=time().$imageName; //Add time stamp before name to give image a unique name
In case assign name in loop, concatenate extra value with increament.
Like :
for($i=1;$i<=$lengthOfLoop;$i++)
{
$imageName="XYZ.png";
$uniqueImageName=time().$i.$imageName; //Add time stamp before name to give image a unique name
}

Replace .dds to .png?

I am looking for an way to replace .dds to .png with php.
The reason is this every images is automaticly inserted into the database with the extension .dds the .dds is an format that the game uses to render its images so i cant replace this.
So what i do is fetch the icons from the database and then the file name must be changed to .png before i insert it into an other database.
I have tried it with preg_replace but i need some help on how to set it up.
$str = str_replace('.dds', '.png', $mystring);
I don't think replacing extension from DDS to PNG is enought. I think you should rather convert from DDS to PNG:
<?php
$url = 'http://server.com/image.dds';
$data = json_decode(file_get_contents('http://api.rest7.com/v1/image_convert.php?url=' . $url . '&format=png'));
if (#$data->success !== 1)
{
die('Failed');
}
$image = file_get_contents($data->file);
file_put_contents('rendered_page.png', $image);

php save image with specific file name not overwriting each other

I hope someone can help me with this php code.
At the moment its just saving an image with the file name "img.png" to the server but with every time a new canvas screenshot is taken the image is just overwritten.
My aim is to create a new unique (like numbered chronological by time taken) file name for the images with every new screenshot and save it on the server.
Here the php code so far:
$data = $_REQUEST['base64data'];
echo $data;
$image = explode('base64,',$data);
file_put_contents('img.png', base64_decode($image[1]));
Thank you.
regards
Try
$filename = 'img_'.date('Y-m-d-H-s').'.png';
file_put_contents($filename, base64_decode($image[1]));
This will save your file with a filename containing the current date and time, e.g.
img_2013-09-19-21-50.png
Try using a session variable to increment a counter like so:
<?php
session_start();
if(!isset($_SESSION['counter'])){
$_SESSION['counter'] = 0;
}
$_SESSION['counter']++;
$data = $_REQUEST['base64data'];
echo $data;
$image = explode('base64,',$data);
file_put_contents('img'.$_SESSION['counter'].'.png', base64_decode($image[1]));
?>
There's several ways to do it, but the easiest is just to add a timestamp/datestamp to the image name. Format the name as you want.
$img_name = 'img'.date('YmdHisu').'.png'; // Date & time with microseconds
$img_name = 'img'.time().'.png'; // unix timestamp
Leave the base64data structure use this one it will work fine.
$fileName = preg_replace('#[^a-z.0-9]#i', '', $fileName);
$image = explode(".", $fileName);
It will give a random number to each image file.
either create a UID using uniqid() function for the filename or create a folder with the name of the username who is uploading the file and leave the original filename. The disadvantage of the first one is that you will have to save the original filename somewhere to show to the user.
https://stackoverflow.com/a/4371988/2701758
**
/* simply for local time first give your continent then '/' then your country's
capital.
*/
date_default_timezone_set('Asia/Dhaka');
$now = new DateTime();
$now = $now->format("Y-m-d H:i:s.u");
$new_name = $now.$image;
/*what you want to add just write with dot,such
$new_name = 'img'.$now.$image;
*/
**

Counting sent files

I have an multiple input sending files and I need guard this images with another name inside my folder called 'home';
So the pictures filing with the name home1.jpg, home2.jpg, etc
So, here is my code:
$file = $_FILES['Filedata'];
$filename_home = "";
$img_array = array($filename);
foreach($img_array as $key=>$value){
$filename_home.="home".$key.".jpg";
}
But this doesn't producing the result.
Any help, will be appreciate
Where does $filename come from? It looks like you want to use $file instead.

Categories