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)) {
}
Related
I stored a picture in the uploads folder of my laravel project, I have an image table that has column name filename as a string type, how can i save the uploaded image location in the filename column and then output in my photos blade file based on the file location in my db
This is the code I used to store my image in my uploads folder
$filename = $file1->getClientOriginalName();
$picture = date('His').$filename;
$file1->move($destinationPath, $picture);
Below is the steps I did regarding upload and retrieve user related images.
For the example, a user upload his profile picture which named myfile.png
So, for upload:
Get the file from $request
Get the file name with getClientOriginalName() (i.e. myfile) and also the file extension with getClientOriginalExtension() (i.e. .png)
Generate some random strings which will be appended to the file name to avoid file name duplication (i.e. fx7ew6)
Append the original file name with the generated random strings along with the extension (i.e. myfile_fx7ew6.png)
Save image file to folder (i.e. public/img) and also save the file name to the database
For retrieve:
You can use <img src="/img/{{FILE_NAME}}"> to show the uploaded image which will result <img src="/img/myfile_fx7ew6.png"> IF you upload the image to public/img.
I'm trying to add the file extension to my file during upload. Since i'm finding it hard to do, i've already hard coded it to in small case .jpeg in my script. How do i get rid of the hard coded file extension and dynamically replace it with the original uploaded one because it could be gif, pdf etc.
<?php
$def_date=strtotime(date('Y-m-d H:i:s'));
$rename = "gwcl_".rand(0,1000000000000).$def_date.".jpg";
$file_loc = $_FILES['file']['tmp_name'];
$folder="../complains_photos/";
$new_file_name = strtolower($rename);
move_uploaded_file($file_loc,$folder.$new_file_name);
echo $new_file_name
?>
$_FILES['file']['tmp_name'] that you're using contains a temporary filename for the uploaded file. But if you want the original filename, including extension, then use $_FILES['file']['name'].
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 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);
I am using library to upload multiple images in php .This library use flash file and a php file . All things are working but problem is that I want to run a query after each image is uploaded to store image detail in database . If i write query in php file after uploading code then It upload images but no error given and no image details are put in database.
Can any one help me that how I add image details in database after uploading image using flash uploader
This is my code
<?
extract($_GET);
$filename = $_FILES['Filedata']['name'];
$temp_name = $_FILES['Filedata']['tmp_name'];
$error = $_FILES['Filedata']['error'];
$size = $_FILES['Filedata']['size'];
/* NOTE: Some server setups might need you to use an absolute path to your "dropbox" folder
(as opposed to the relative one I've used below). Check your server configuration to get
the absolute path to your web directory*/
if(!$error){
copy($temp_name, '../dropbox/'.$filename);
$news_query="insert into tbl_news(img_id,headline,caption,news_catgory_id,shooting_date) Values('0000','News_Headline','News_Caption',
'New_Category','now()')";
mysql_query($news_query) or die(mysql_error());
}
?>
all code working but query data is not updated in database
May be it's a sql error...if your "shooting_date" field is numeric or date, you can't set it's value like this: 'now()', you must strip the quotes.