I made a form in which a user can upload images. I am storing the copy of the uploaded image into a folder named "pics"(full path is C:\wamp\www\pics). I am also storing the name of the image in my database table.
I want to display all images in a page. I created a script. Used while and foreach loop to fetch the name of the image and then added the name in the image tag. It's now displaying a blank broken box.
The address of the image is correct. I select "Copy image location" by right clicking on the pic and it's displaying the correct path. eg: C:\wamp\www\pics\abc.JPG
Here's the code:
<?php
//Including SQL Connection Credentials
require_once("sql_connection.php");
$sql_fetch_pics = "SELECT name FROM pics";
$query_fetch_pics = mysql_query($sql_fetch_pics);
while($fetched_pic = mysql_fetch_assoc($query_fetch_pics)){
foreach($fetched_pic as $display_pic){
$full_img_name = dirname(__FILE__)."\pics\\".$display_pic;
echo "<img src='".$full_img_name."'/>";
}
}
?>
The src tag should be URL, not filesystem path:
$full_img_name = "/pics/".$display_pic;
Solution:
$full_img_name = "/pics/".$display_pic;
Explanation goes here: dirname(__FILE__) on localhost
Related
I have the following shortcode which gets the images of that post (that will be in a folder named as the post's id ) and displays them echoing as many tag images as needed.
session_start();
$postID = $_SESSION['post_ID'];
$upload_dir = wp_upload_dir();
$path = $upload_dir['basedir'].'/'.'gallery/'.$postID;
if(file_exists($path)) {
$pathFile = $path.'/*';
$images = glob($pathFile);
if(!empty($images)) {
foreach($images as $image){
$alt = end(explode('/', $image));
echo '<img src="'.$image.'" alt="'.$alt.'" />';
}
} else echo 'No hay imágenes.';
} else echo 'No hay imágenes.';
?>
The problem is that the images aren't displayed, i get the followin error in the brower's console GET https://path-to-theme/wp-content/uploads/gallery/1658/wwg.jpg 404, per every image and i get this icon:
image
I have no idea why my images aren't displayed the images, the path is correct, the images are there, complitely no idea what I'm missing. It's not a permissions problem as i tried giving 755 permissions to the directories starting from /uploads/.. and so on including the images.
I'm going to assume that the URL you reference in your question is actually https://yourdomain/wp-content/uploads/gallery/1658/wwg.jpg and not https://path-to-theme/wp-content/uploads/gallery/1658/wwg.jpg. Right?
If that's the case, then it should be able to pull open that file. The first thing I'd check is whether you actually have the file located where you think it is: /wp-content/uploads/gallery/1658/
If the file is actually in that folder, and you can access it from your browser at https://yourdomain/wp-content/uploads/gallery/1658/wwg.jpg then my next suggestion would be to check the permission of each of the folders leading up to the file and make sure they are set to 755, and then make sure your file is set to 644.
Here are instructions on changing file permissions.
Let me know if that helps?
I fixed the problem. The issue wa sthat i wasn't uploading the images as wp wants me to, therefore the images, even they were in the folder, couldn't be displayed. So I had to change the way i uploaded them, by using wp functions and specifying the mime_type etc so wp could store their metadata in it's db in order to be able to acknowledge them and display them in a future.
I have a hosted website and i want to upload my images to image directory and link the image to mysql database .
What i did:
Creating form as :
<form method=POST align=center enctype="multipart/form-data">
<input type=file name=image>
Php code :
$uploaddir = 'images/';
// echo $uploaddir;
$target = "$uploaddir".basename($_FILES['image']['name']);
echo $target;
$image = $_FILES['image']['name'];
$sql="insert into News2(image_News2)
values($image)";
$res=mysqli_query($con,$sql);
copy($_FILES['tmp_name']['name'],$target,true);
What this code actually do is only inserting the image into the database not copying the image into the images folder.
What i searched for and found :
You shouldn't specify a http:// URL as $uploaddir but a path relative to the path where the php script is running from.
So i what i really wanted to do is :
1- Copying the image into image folder directory on website.
2- Linking the copied image to the database.
you're missing the name ïmage"
not:
copy($_FILES['tmp_name']['name'],$target,true);
but:
move_uploaded_file($_FILES['image']['tmp_name'], $target);
And maybe you should try the move first, before inserting it in the database, should that action fail (or filesize = 0) etc.
do check on both the file actions and the db actions, should they fail: rollback
extra:
is only 1 column present in your database table?
is it wise to use the uploaded file's name? Duplicates can happen.
I would after checking the file insert the original name into the database. Get the autoincrement id of that record. rename the file to ./1234.jpg
through the database you can then later use the original name. But this numbering might go too var for a simple application
PHP Code :
if (isset($_FILES['image']['name']) && $_FILES['image']['error']
== 0) {
$uploads_dir = '/uploads';
$temp_file = $_FILES['image']['tmp_name'];
$name = basename($_FILES["image"]["name"]);
if(move_uploaded_file($temp_file, "$uploads_dir/$name")){
//Fire Insert Query For Insert image name in your database
}
else
{
echo "Error !! , Your image not uploaded.";
}
}
?>
as per your given data you must close Form tag then give form action ="" attribute to specify url . and if your oprating system is linux then check folder permission in set or not , also check your database table if field name is set unique or not ?
then write below code in given url file
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).
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 want to be able to open the provided URL (which is done via a form) that is an URL that will allow the server to save the file into a directory, for example:
http://www.google.co.uk/intl/en_com/images/srpr/logo1w.png
I want to save that logo into this directory:
img/logos/
Then it will add it to the database by giving it a random file name before so, e.g.
827489734.png
It will now be inserted to the database with the following:
img/logos/827489734.png
I do not want to use cURL for this, I like to work with fopen, file_get_contents, etc...
Cheers.
EDIT
$logo = safeInput($_POST['logo']);
if(filter_var($avatar, FILTER_VALIDATE_URL))
{
$get_logo = file_get_contents($logo);
$logo_directory = 'img/logos/';
$save_logo = file_put_contents($logo_directory, $logo);
if($save_logo)
{
$logo_path = $logo_directory . $save_logo;
A part of this code I need helping...
You need to specify a full file name when doing a file_put_contents(). A pure directory name won't cut it.