I want to show an IMG in my Images folder,
I have the correct path, but I get a question when I show the image.
Code:
$filename = 'Images/img1.png';
if (file_exists($filename)) {
echo '<img src="' . $filename . '"/>';
echo "The file $filename exists";
} else {
echo "The file $filename does not exist";
}
Did you try to check file permissions? This is the most recent problem, when you see that the file exists but it cannot be displayed because of permissions.
If you are using IIS try to assign read permissions for IIS_IUSRS on the file or images folder.
If you are using Linux, just change file permissions for the file (chmod command). As a starting point try to assign 777 permissions to the file just to check if it changed something.
I have tried the same code you have give above and its working perfect. So, try after giving permission to your images folder.
And, you can also check the exact issue by executing the image path url prefixed with your local directory path in the browser.
Use absolute path...
$filename = '/absolute/path/to/your/folder/www/Images/img1.png';
/*
* OR better...
* $filename = dirname(__FILE__).DIRECTORY_SEPARATOR."Images".DIRECTORY_SEPARATOR."img1.png";
* dirname(__FILE__) is the directory of your current script... If you have level before use:
* dirname(dirname(__FILE__))
*/
if (file_exists($filename)) {
echo '<img src="' . $filename . '"/>';
echo "The file $filename exists";
} else {
echo "The file $filename does not exist";
}
Related
I am trying to upload photos in to folder uploads and its path to be recorded under photo in DB. This is my code:
$folder ="uploads";
$destFile = $folder . basename($_FILES["photo"]["name"]);
$sourdeFile = $_FILES["photo"]["tmp_name"];
if(move_uploaded_file($sourdeFile,$destFile)){
echo "File has been uploaded";
$photo = $destFile;
}else{
echo $_FILES['photo']['error'];
$photo = "images/default.png";
}
When I upload photos they successfully uploaded into folder but the problem is its path recorded as follow :
uploads42141402_1866830986743601_8538143552767524864_n.jpg
But to view photos in a page there should be \ next to uploads. So I tried to change my code as follow.
$folder = "uploads\";
But it generates this error
Can anyone say how to fix this ?
How about
$folder ="uploads/";
I've tried and it works. Maybe $folder ="uploads" . DIRECTORY_SEPARATOR; will better than "/".
For the error its looks uploads folder is not there or it not have permission
create a upload folder or try with absolute path
you can make use of dirname(__FILE__) or $_SERVER['DOCUMENT_ROOT'] for creating dynamic path
I am having trouble uploading file on server.
My code:
$path = "produkty/$kategorie/$last_id/";
if(!is_dir($path)){
mkdir($path, 0777, true);
}
if (is_dir($path) && is_writable($path)) { echo "ok"; }
And it returned ok.
The upload code looks like this:
move_uploaded_file($_FILES["uvodniobr"]["tmp_name"], $path."uvodniobr.".$ext)
If I echo out:
echo $path."uvodniobr.".$ext;
It will return this: produkty/kategorie/35/uvodniobr.png
Things like checking file size, extension works fine, it just won't upload.
Do you know what could be wrong with my code? Thank you
SOLVED
EDIT: I was trying to upload file over 2MB without changing php.ini in xampp/php/php.ini upload_max_filesize=2M.
Thank you for your answers.
I guss problem is because you're using relative path in move_uploaded_file while it needs absolute path So
You should use document_root to get absolute path like this:
$target_path = $_SERVER['DOCUMENT_ROOT'] . '/' .$path; // if not work try $target_path = $_SERVER['DOCUMENT_ROOT'] . $path;
move_uploaded_file($_FILES["uvodniobr"]["tmp_name"],
$target_path."uvodniobr.".$ext);
Also it's better to surround it with if to check the result
$target_path = $_SERVER['DOCUMENT_ROOT'] . '/' .$path; // if not work try $target_path = $_SERVER['DOCUMENT_ROOT'] . $path;
if (move_uploaded_file($_FILES["uvodniobr"]["tmp_name"],
$target_path."uvodniobr.".$ext)) {
echo "File is valid, and was successfully uploaded.\n";
} else {
echo "Upload failed";
}
Helper link move_uploaded_file & relative path
It was my fault, I forgot to change upload_max_filesize=2M to upload_max_filesize=16M in xampp/php/php.ini
My bad, sorry and thank you for your effort I really appreciate it.
So i want my page to show the image whose path I am getting from the mysql database and displaying on the same screen. This is my code, I have tried everything, please let me know where I'm going wrong.
while ($row = mysqli_fetch_array($return_data)) {
echo "ID:".$row['demo_id']."<br>";
echo "Name: ".$row['demo_name']."<br>";
echo "Version: ".$row['demo_version']."<br>";
echo "Details: ".$row['demo_details']."<br>";
echo "File Link: ".$row['file']."<br>";
$new = $row['file'];
echo '<img src = \"$new\"/>';
}
mysqli_free_result($return_data);
echo "Data retrieved successfully!"."<br>";
?>
<img src = "<?php echo $new?>">
echo "File Link: " returns me the whole path of the uploaded file.
How do I render the image at that path in the same page?
neither of the image tags are working. Thanks in advance!
edit
File Link: C:/Apache24/htdocs/demo_webpages_project/neweruploads/footer.jpg
this is the path I get as an output.
Basically this is the folder where I have uploaded the image from another php file
<?php
//this module is used to temporarily store the uploaded file to the server
$target_dir = "random/"; //we randomly assign a value to the upload target directory
$target_file = $target_dir . basename($_FILES["image_file"]["name"]); /*here ["name"] is the original name of the file before it was updated
target file is assigned this name by appending it to the $targer_dir
now target_file is the uploaded file name along with the path*/
$imageFileType = pathinfo($target_file,PATHINFO_EXTENSION);/*this returns various kind of info about the path for eg Directory, Basename
in our case it returns extension of the file*/
//this sub module is to check whether the file is really an image file
if(isset($_POST["submit"])) { //isset is used to confirm whether the value has actually being submitted
$check = getimagesize($_FILES["image_file"]["tmp_name"]);/*here ["tmp_name"] is the location of temporary file on server
getimagesize confirms image format by returning dimensions etc*/
if($check !== false) {
echo "A file is of an image format<br>";
}
else {
echo "The file is not an image!<br>";
}
}
//Test module to upload files to a destination directory and check whether they have been uploaded or not
if (is_uploaded_file($_FILES['image_file']['tmp_name']) && $_FILES['image_file']['error']==0) { /*code to check two things: 1. whether the file exists in the temp memory 2. whether the file has any error*/
$path = 'C:\Apache24\htdocs\demo_webpages_project\neweruploads\\' . $_FILES['image_file']['name']; /*this sets the destination directory(along with expected file name)*/
if (!file_exists($path)) { //if the file does not exists at that path
if (move_uploaded_file($_FILES['image_file']['tmp_name'], $path)) { //use the php file move function to move it
echo "The file was uploaded successfully."; //success
}
else {
echo "The file was not uploaded successfully."; //failure
}
}
else {
echo "File already exists. Please upload another file."; //detects existence of file with exact same name
}
}
else {
echo "The file was not uploaded successfully."; //if any problem with original uploading
echo "(Error Code:" . $_FILES['image_file']['error'] . ")";
}
?>
Does this help?
edit 2
http://localhost:8080/demo_webpages_project/download.php?project_name=footer&version=&submit=Search
this is my local directory path.
the solution you provided is allowing me to read images which are in the demo_webpages_project folder pointing directly there), not to neweruploads folder
If your uploaded files are stored in the neweruploads subdirectory, then replace this code:
$new = $row['file'];
echo '<img src = \"$new\"/>';
By this one :
$new = basename( $row['file'] ); // GET FILE NAME ONLY, GET RID OF PATH.
echo '<img src = \"neweruploads/$new\"/>'; // FILENAME WITH UPLOAD PATH.
▲
I'm attempting to move an uploaded image (from Android) that is to be renamed via the PHP below in the second example so that their names cannot conflict. The original example below uploads files correctly but can have naming conflicts. The error that I'm experiencing is that the move_uploaded_files function fails, which I'm unsure as to why. The directory appears the same but I could be wrong and the problem is that image is never moved from the temp directory. Above all else, I think this is just a directory issue since the original example works. Please let me know if you need more information. The example I'm going by is located here: How to rename uploaded file before saving it into a directory?
Original:
$uploaddir = './appphotos/';
$absPath = 'https://'.$_SERVER['HTTP_HOST'].'/complaint_desk/appphotos/';
$file = basename($_FILES['userfile']['name']);
$uploadFile = $file;
$newName = $uploaddir . $uploadFile;
New Attempt:
$temp = explode(".",$_FILES["userfile"]["name"]);
echo json_encode($temp);
$newfilename = rand(1,99999) . '.' .end($temp);
echo json_encode($newfilename);
$uploadFile = move_uploaded_file($_FILES["userfile"]["name"], "/var/www/complaint_desk/appphotos/" . $newfilename); echo json_encode($uploadFile);
You should use the function as follow:
if(move_uploaded_file($_FILES["userfile"]["tmp_name"], "./appphotos/" . $newfilename)) {
echo json_encode($uploadFile); // why do you want to encode it?
} else {
echo 'File failed to move';
}
Always check the result of move_uploaded_file(). Also, the file is located at $_FILES["userfile"]["tmp_name"] before moving.
Also, $absPath is incorrect. It shouldn't start with http protocol. It should look like /var/www/complaint_desk/appphotos/ or C:/complaint_desk/appphotos/.
<?php
//set where you want to store files
//in this example we keep file in folder upload
//$HTTP_POST_FILES['ufile']['name']; = upload file name
//for example upload file name cartoon.gif . $path will be upload/cartoon.gif
$path= "upload/".$HTTP_POST_FILES['ufile']['name'];
if($ufile !=none) {
if(copy($HTTP_POST_FILES['ufile']['tmp_name'], $path)) {
echo "Successful<BR/>";
//$HTTP_POST_FILES['ufile']['name'] = file name
//$HTTP_POST_FILES['ufile']['size'] = file size
//$HTTP_POST_FILES['ufile']['type'] = type of file
echo "File Name :".$HTTP_POST_FILES['ufile']['name']."<BR/>";
echo "File Size :".$HTTP_POST_FILES['ufile']['size']."<BR/>";
echo "File Type :".$HTTP_POST_FILES['ufile']['type']."<BR/>";
echo "<img src="$path" width="150" height="150">";
} else {
echo "Error";
}
}
?>
it is an toturial form here http://tutorialblog.info/2010/08/06/php-upload-single-file.html
when i tried in my local environment. it displays an error? what's wrong with the code? thank you.
first, i think this is wrong. if($ufile !=none) it should be if($_POST['ufile' !=none)
am i right?
I agree with Stephen, this is not a good example, you also need to validate your upload. you are not going to allow anyone to upload anything.
but if you want just find the issues: well HTTP_POST_FILES is no longer accepted by PHP newer versions. should be $_FILES
$ufile !=none can be if ($_FILES['ufile']['tmp_name'])
copy($HTTP_POST_FILES['ufile']['tmp_name'], $path) better be move_uploaded_file($_FILES['ufile']['tmp_name'], $path)
Your html form enctype should be enctype="multipart/form-data"
the $path path should have the right permission