This script is giving error if name field and file field is same. That if I want to upload file with a.jpg and name field is also a than its giving error of rename. Let me know to remove this problem and help to remove previous file.
$username=$name ;
move_uploaded_file($_FILES["pic"]["tmp_name"],"albumpic/".$_FILES["pic"]["name"]);
$ext=substr($_FILES["pic"]["name"],strpos($_FILES["pic"]["name"],"."));
if(file_exists("albumpic/$username$ext")) { unlink("albumpic/$username$ext"); }
rename( "albumpic/".$_FILES["pic"]["name"],"albumpic/$username$ext");
$newphoto="$username$ext";
//var_dump($photo);
$err="";
This is horribly bad code. You first move the user-provided file, which overwrites anything that was there before. You extract the file's extension in an unreliable manner (think of what happens if someone uploads mypic.jpg.exe). You then rename the uploaded file AFTER it's possibly trashed something what was there before.
Consider the case that you've got users "Joe" and "Fred" with profile pictures "joe.jpg" and "fred.jpg". What if Fred uploads a new profile picture called "joe.jpg". Your system will destroy Joe's image.
Try this instead:
$ext = pathinfo($_FILES['pic']['name'], PATHINFO_EXTENSION);
if (file_exists("albumpic/$username$ext")) {
unlink("albumpic/$username$ext");
}
if (!move_uploaded_file($_FILES['pic']['tmp_name'], "albumpic/$username$ext")) {
die("Unable to move user $username's picture to album directory");
}
Related
I have images with user id (eg.img299 etc.). This images are already place in a folder called images. When user login, I create image name with user id like this img299(299 is the user id).
After I create image name, I need to check this image name is already exists or not in images folder. If image is exists, I want to show that image and if not just only show underline.
So, I try like this:
$filename = "img".$userID;
$filepath = "http://www.example.com/images/".$filename.".gif";
var_dump(getimagesize($filepath));//for testing
if(getimagesize($filepath)) {
echo "<img src = 'http://www.example.com/images/".$filename.".gif'>";
} else {
echo "____________________________";
}
The above code is work if the image is exists. But the problem is, if the image is not exists under images folder, getimagesize return like this:
So, the result is true and that line echo "http://www.example.com/images/".$filename.".gif'>"; is work and image can't show correctly.
That is the problem for me. I want to show only underline if the image is not exists.
So, I also try with if(file_exists($filepath)) and it always return false even if the image is exists.
How can I check image is exists or not. I will appreciate any suggestion.
Use PHP's file_exists with the local path to the file to check if it exists. I.e. do not use the full URL (example.com/...) but the path on the filesystem relative to the file you're executing (e.g. ../images/filename.png).
I'm trying to make a website where you can upload flash .swf files. It works great and all except whenever a user uploads a flash file name with japanese or any other asian characters (tested with Japanese characters) it gives me the name wrong.
So this is the file:
Before it starts moving the file im putting the information in the database.
Database:
flash_id | flash_name | not relevant | flash_size | flash_date
After putting the flashes information in the database I move the file from the tmp folder to another folder where I store the uploads.
If you compare the file names from the first 2 pictures with the third one you can see it isn't the same.
Here is the code:
The variables map, filename and ext below are the values that were used in the moving process.
$map = 62;
$filename = $_FILES['upfile']['name'];
$ext = swf;
$str = "../uploads/$map/$filename.$ext";
if (!move_uploaded_file(
$_FILES['upfile']['tmp_name'],
//sprintf('../uploads/%s/%s.%s',$map,$filename,$ext)
$str
)) {
// Failed to move uploaded file.
throw new RuntimeException(4);
} else {
try {
$insq = $pdo->prepare('INSERT INTO flashes (flash_name, flash_size) VALUES (?,?)');
$insq->bindParam(1, $filename);
$insq->bindParam(2, $filesize);
$insq->execute();
} catch (PDOException $ex) {
throw new RuntimeException(999);
}
}
// File is uploaded successfully.
echo 10;
I could not find a solution for this. So instead of trying to fix this I followed tttony's comment and made it so that the file_names are randomized and the file_name before randomizing is put in the database. So we can show it on our website with its original name however when we are going to download it we will receive a randomized file_name. However if you use the html download="original_file_name" attribute you can still make the user download it with the original file_name.
Got the server, got the domain, got the code, getting the images successfully, making the products for the customers from the image files they upload. Yay!
Problem: all my image names are image_0001 etc.
Customers can't rename image files from iPhones and do not care to from PCs.
So I was thinking about putting a short form on the upload page asking for customer's last name and having the PHP code attach that name to the image file(s) being uploaded.
If it's not possible, I'm sorry for the inconvenience.
You can rename files after they have been saved to your server, check out the PHP manual for the rename function - http://www.php.net/manual/en/function.rename.php, or just while you are moving them from the tmp directory, you can specify a different name for the uploaded file. See http://www.php.net/manual/en/function.move-uploaded-file.php
Be careful to include something in your code for dealing with naming conflicts.
This one might help :
$imagename = basename($_FILES['file']['name']);
$ext = pathinfo($imagename , PATHINFO_EXTENSION); //we want to change the file name but not the extension
$newImagename= $imageName.$username.'.'.$ext; //assuming you hold the username in $username
if (move_uploaded_file($_FILES['file']['tmp_name'], "/path/{$newImagename}"))
{
....
}
I have a problem with saving PDF files to folders on my server. The code worked at one time and now it doesn't. What I want it to do is to check if someone is trying to upload a PDF when a form is submitted, and if there is a PDF in the file field it uploads it and then saves the path to the mysql database. Code is below:
if (!empty($_FILES['pdf'])){
$idir = "../files/PDF/"; //my directory file is supposed to be saved in
$randomd=rand(0000000,9999999); //creates a random number as filename
$domain = "http://".$_SERVER['HTTP_HOST'];
$file_ext = strrchr($_FILES['pdf']['name'], '.'); grabs file extension. my code checked if the file was a pdf a different way and neither seems to work.
$destination=$randomd.$file_ext; //new filename
if ($file_ext=='pdf') {
move_uploaded_file($_FILES['pdf']['tmp_name'], "$idir" . $destination);
$pdf= $domain."/files/PDF/".$destination; } else { echo("File type not supported.");
mysql_query("UPDATE tbl_listings SET pdf='$pdf' WHERE listing_id='$lid'");
}
The if not empty does not work and it always tries to upload a file, but when I check the folder nothing is in there and it doesnt update the mysql.
$_FILES['pdf'] will never be empty(when the form has been submitted), no matter if a file has been selected or not, it will always return an array.
Check $_FILES['pdf']['error'] , it will be 4 when no file has been uploaded.
I have searched far and wide on this one, but haven't really found a solution.
Got a client that wants music on their site (yea yea, I know..). The flash player grabs the single file called song.mp3 and plays it.
Well, I am trying to get functionality as to be able to have the client upload their own new song if they ever want to change it.
So basically, the script needs to allow them to upload the file, THEN overwrite the old file with the new one. Basically, making sure the filename of song.mp3 stays intact.
I am thinking I will need to use PHP to
1) upload the file
2) delete the original song.mp3
3) rename the new file upload to song.mp3
Does that seem right? Or is there a simpler way of doing this? Thanks in advance!
EDIT: I impimented UPLOADIFY and am able to use
'onAllComplete' : function(event,data) {
alert(data.filesUploaded + ' files uploaded successfully!');
}
I am just not sure how to point THAT to a PHP file....
'onAllComplete' : function() {
'aphpfile.php'
}
???? lol
a standard form will suffice for the upload just remember to include the mime in the form. then you can use $_FILES[''] to reference the file.
then you can check for the filename provided and see if it exists in the file system using file_exists() check for the file name OR if you don't need to keep the old file, you can use perform the file move and overwrite the old one with the new from the temporary directory
<?PHP
// this assumes that the upload form calls the form file field "myupload"
$name = $_FILES['myupload']['name'];
$type = $_FILES['myupload']['type'];
$size = $_FILES['myupload']['size'];
$tmp = $_FILES['myupload']['tmp_name'];
$error = $_FILES['myupload']['error'];
$savepath = '/yourserverpath/';
$filelocation = $svaepath.$name.".".$type;
// This won't upload if there was an error or if the file exists, hence the check
if (!file_exists($filelocation) && $error == 0) {
// echo "The file $filename exists";
// This will overwrite even if the file exists
move_uploaded_file($tmp, $filelocation);
}
// OR just leave out the "file_exists()" and check for the error,
// an if statement either way
?>
try this piece of code for upload and replace file
if(file_exists($newfilename)){
unlink($newfilename);
}
move_uploaded_file($_FILES["fileToUpload"]["tmp_name"], $newfilename);