Image is stored in database (as varchar(255)), but on page isn't shown.
This is code that stored image, and put's image in folder:
if (isset($_FILES["image"])) {
$title = date("dmyHms") . "_" . $_FILES["image"]["name"];
$path = "img/profile/" . $_POST["id"] . "_" . $title;
move_uploaded_file($_FILES["image"]["tmp_name"], $path);
}
And this is to display image:
foreach ($conn->results() as $conn):
$img = $_SERVER["CONTEXT_DOCUMENT_ROOT"] . $path . "img/profile" . $conn->id . "_" . $conn->image;
if (file_exists($img)) {
$image = $path . "img/profile/" . $conn->id . "_" . $conn->image;
} else {
$image = $path . "img/noimage.png";
}
<?php endforeach; ?>
When I print_r $img, it shows right path to image and still image is not displayed.
Display:
<?php echo $image; ?>
Where did you echoing your image? If it is inside foreach, you need to change your code little bit...
$image = '<img src="'.$path . "img/profile/" . $conn->id . "_" . $conn->image . '" />';
Hope, will work perfectly for you.... TQ
The following line is not correct:
foreach ($conn->results() as $conn):
Don't overwrite $conn. Change the variable to something different like $result
Related
I am trying to upload a tmp file and then copy it. But it is not appearing in the second folder, is there an overlay or something I have to watch?
$uploadPath = '/.../..../image/';
$uploadPathpreview = '/.../..../blog/';
if ($counter == 0){
$preview_file = $uploadPath . DS . 'preview_' . date("Ymd-Hi-") . $img_name;
$preview_file_save = $uploadPathpreview . DS . 'preview_' . date("Ymd-Hi-") . $img_name;
if(move_uploaded_file($tmp_name, $preview_file)){
$tmp = $preview_file;
$new = $preview_file_save;
}
copy($tmp,$new);
The file in image exists by the way. And I tried it without the if()
I'm trying to make a PHP script that changes the featured picture of some item.
The general principle is that it when $_POST['featured'] is set, it renames that picture to featured.jpg and renames all other pictures to their hash_file value. However, it works well the first time when there is no featured picture set, but when I try to change it from one featured picture to another, the previous featured picture gets removed and the new one doesn't get renamed to featured.jpg. All pictures are in the same folder.
Here is the relevant code:
if (isset($_POST['featured'])) {
$id = $_POST['id'];
$slike = glob('../img/uploads/'.$id.'/*.{jpg,png}', GLOB_BRACE);
if ($slike != null) {
foreach ($slike as $slika) {
$path = realpath($slika);
$name = basename($path);
if($name == basename($_POST['featured'])){
if(!file_exists(dirname($path) . '/featured.jpg')){
rename($path, dirname($path) . '/featured.jpg');
}else{
rename(dirname($path) . '/featured.jpg', dirname($path) . '/' . hash_file('md5', $path) . '.jpg');
rename($path, dirname($path) . '/featured.jpg');
}
}else{
rename ($path, dirname($path) . '/' . hash_file('md5', $path) . '.jpg');
}
}
}
}
I am slowly working on a image uploader, and wondering why when echoing my renamed files, its giving me a single character instead of the whole thing.
Any reason it would be doing that?
It does tho, successfully upload the image as a phil_546d196082606.jpg with a different number for each image
Here is my code
<?php
if (isset($_POST['addpart'])) {
$image = $_FILES['images']['tmp_name'];
$name = $_POST['username'];
$i = 0;
foreach ($image as $key) {
$fileData = pathinfo(basename($_FILES["images"]["name"][$i]));
$fileName = $name .'_'. uniqid() . '.' . $fileData['extension'];
move_uploaded_file($key, "image/" . $fileName);
copy("image/" . $fileName, "image_thumbnail/" . $fileName);
$i++;
}
echo 'Uploaded<br>';
$fileName1 = $fileName[0];
$fileName2 = $fileName[1];
$fileName3 = $fileName[2];
echo 'Main Image - '.$fileName1.'<br>';
echo 'Extra Image 1 - '.$fileName2.'<br>';
echo 'Extra Image 2 - '.$fileName3.'<br>';
echo '<hr>';
}
?>
$filename is a string and strings in php are arrays where each letter has an index $filename[o] is the first letter and so on.Use
$filename[]=$name .'_'. uniqid() . '.' . $fileData['extension'];
Try the below block of code
$fileName[] = $name .'_'. uniqid() . '.' . $fileData['extension'];
move_uploaded_file($key, "image/" . end($fileName));
copy("image/" . end($fileName), "image_thumbnail/" . end($fileName));
$fileName = $name .'_'. uniqid() . '.' . $fileData['extension'];
Filename is the string. It is : $name . number.
Like philip12345.
So if we have:
philip
012345
$fileName[0] = p
$fileName[1] = h
Also you overwrite filename in each loop. Try to save it to an array and print it, here is some code:
$fileNames = array();
foreach ($image as $key)
{
$fileName = $name .'_'. uniqid() . '.' . $fileData['extension'];
fileNames[$i] = $fileName;
}
echo $fileNames[0];
echo $fileNames[1];
echo $fileNames[2];
You could also use a foreach loop to go over the array with the filenames and print each element, this is cool because it will works with any number of images, not just 3:
foreach ($fileNames AS $key2)
{
echo ($key2);
}
function givemeG($path) {
$files = glob($path . '*.jpg');
$cs=" class=\"act\"";
foreach($files as $img) {
echo "<img" . $cs . " src=" . "\"" . $img . "\"" . " alt=\"img\">";
$cs="";
echo "\n";
}
}
$path = "frederic/";
givemeG($path);
This writes img tags for all images inside frederic folder
How can I do the same but instead of frederic folder I need the images from another domain (the same host server).
Simply replacing path doesn't work:
$path = "http://www.codee.site50.net/frederic/";
givemeG($path);
I want to be able to present the photo of a user. That user may be of the type Doctor, Nurse or Patient and each person belonging to each of those types has a distinct ID which identifies him. I have the photos of each type of users separated in three folders inside the directory /images/user_upload/ and a given user may or may not have a photo. If he has a photo, I want to present the photo with a name which is the same as his ID (Note that I don't know the extension of this file, only the name). If he doesn't I want to present a default image. I have a function called printUserPhoto for this end.
function printUserPhoto()
{
$path = '/images/user_upload/' .ucfirst($_SESSION['listtype']). '/' .$_SESSION['id']. '.*';
$source = glob($path);
if(empty($source))
echo '<img src="images/default_profile_img.png" />';
else
echo '<img src="',$source[0],'" />';
}
Although, I always get an empty array in the variable $source, even though the image exists. What am I doing wrong?
This way with glob
function printUserPhoto()
{
$pathToDocumentRoot = $_SERVER['DOCUMENT_ROOT'];
$path = $pathToDocumentRoot . '/images/user_upload/' .ucfirst($_SESSION['listtype']). '/' .$_SESSION['id']. '.*';
$source = glob($path);
if(empty($source)){
echo '<img src="images/default_profile_img.png" />';
} else {
echo '<img src="',$source[0],'" />';
}
}
Maybe limit the filetypes user can upload. With that you could do something like this:
function printUserPhoto()
{
$pathToDocumentRoot = $_SERVER['DOCUMENT_ROOT'];
$path = $pathToDocumentRoot . '/images/user_upload/' .ucfirst($_SESSION['listtype']). '/' .$_SESSION['id'];
$file = "images/default_profile_img.png";
if(file_exists($path . '.jpg')){
$file = $path . '.jpg';
}else if(file_exists($path . '.png')){
$file = $path . '.png';
}else if(file_exists($path . '.gif')){
$file = $path . '.gif';
}
echo '<img src="' . $file . '" />';
}
Also as being said by #Phil you are now referring to the file system root / being absolute path not relative to your document root.