Reputation: 335
I have the following script that generates a random timestamp for my filename.
$newfilename = round(microtime(true)) . ".jpg";
However, the filename that is uploaded to my mySQL database is different from the one recorded in my directory, which causes the image to fail to display.
For example, on one occasion, the image in the database is recorded as 1441743610.jpg, while the image in the directory is 1441743609.jpg.
This is my code:
session_start();
$username = $_SESSION['username'];
$file_dir = "userpictures/$username" ;
mkdir($file_dir, 0777, true);
// $_FILES["file"]["name'] is the file that is uploaded by her
$filePath = $_FILES["file"]["name"];
$image = imagecreatefrompng($filePath);
$bg = imagecreatetruecolor(imagesx($image), imagesy($image));
imagefill($bg, 0, 0, imagecolorallocate($bg, 255, 255, 255));
imagealphablending($bg, TRUE);
imagecopy($bg, $image, 0, 0, 0, 0, imagesx($image), imagesy($image));
imagedestroy($image);
$quality = 100; // 0 = worst / smaller file, 100 = better / bigger file
imagejpeg($bg, $filePath . ".jpg", $quality);
imagedestroy($bg);
// this chunk here converts the file to jpg
$newfilename = round(microtime(true)) . ".jpg";
// round(microtime) generates a randomized number.
// check format of image before uploading
$imageFileType = pathinfo($newfilename,PATHINFO_EXTENSION);
if ($imageFileType != "jpg" && $imageFileType != "png" && $imageFileType != "jpeg"
&& $imageFileType != "gif" )
{
echo "Sorry, only JPG, JPEG, PNG & GIF files are allowed.";
$uploadOk = 0;
}
else
{
$uploadOk = 1;
}
// if any errors have occured (determined by $uploadOk), the critical "move_uploaded_files" php function is not activated
if ($uploadOk == 1)
{
$filename = $newfilename;
move_uploaded_file($_FILES["file"]["tmp_name"], "user_pictures/$username/" . $filename);
}
Would appreciate advise on why this is happening. Note that this behaviour is not consistent and it works most of the time., but thats not acceptable to me.
EDIT:
This is the code where I upload the $filename
to the database (as well as other data)
$con = mysqli_connect("FARM.mysql.anyhost.com", "user", "password");
if (!$con) {
die("Something went wrong. Don't worry, just refresh the page"); }
mysqli_select_db($con, "FARM");
if (mysqli_select_db($con, "FARM") == false) {
die("Something went wrong. Don't worry, just refresh the page"); }
$username = $_SESSION['username'];
$post = mysqli_real_escape_string($con, $_POST['postdescription']);
$timestamp = date("jS \of F Y h:i:s A P");
$user_uploadposts = "INSERT INTO $username (PostingTime, Image, Description) VALUES ('$timestamp','$filename', '$post')";
mysqli_query($con, $user_uploadposts);
mysqli_close($con);
Upvotes: 1
Views: 134
Reputation: 465
I don't know how your query is when saving the filepath in the database but according to the behaviour you're explaining here :
Note that this behaviour is not consistent and it works most of the time., but thats not acceptable to me.
the problem is that you're not using $newfilename when saving to the database, but "recreating" the filename again with $newfilename = round(microtime(true)) . ".jpg";
This causes that in the meanwhile microtime is incrementing and the names are not equal anymore. The reason why it sometimes works is because the script goes sometimes faster through. To avoid this you just have to use $newfilename instead of creating the variable again.
h.u.
Upvotes: 1