PHP - Cant see my image - php

Im beginner as php programmer,so ive got various problem , one of them after I made an easy upload system,when I want to see the picture which I uploaded to the server,doesnt shown.
Here's the code:
<?php
$target_path = "uploads/";
$target_path = $target_path . basename( $_FILES['file']['name']); //the target path that the file will move
/*Moving the picture,to the server if successed the condition will be true*/
if(move_uploaded_file($_FILES['file']['tmp_name'], $target_path))
{
echo"התמונה הועלתה בהצלחה!";
echo"</br></br>";
echo $target_path;
$show_photo = "<img src='$target_path' alt='Picture' class='photo' />";
function AddToAllPhotosList()
{
$myFile = "photosorder.php"; //ListOfAllPhotos
$fileHander = fopen($myFile,'a') or die ("אין אפשרות לפתוח את הקובץ");
}
}
else
echo "Error.";
?>
The Line -"<img src='$target_path' alt='Picture' class='photo' />" , Doesn't shows the photo why ?

$show_photo = "<img src='$target_path' alt='Picture' class='photo' />";
Replace To:
echo "<img src='$target_path' alt='Picture' class='photo' />";

The HTML is being saved to variable $show_photo, but is not being echoed.
...
$show_photo = "";
echo $show_photo;
...

It looks like you aren't actually outputting the tags. You are currently storing them in the $show_photo variable, which is causing you problems. Either do
echo $show_photo;
after you've declared it, or replace line 10 with the following:
echo "<img src='$target_path' alt='Picture' class='photo' />";
Note - the type of quotes used are important!

Add an echo of $show_photo which should show the html you intend to show to appear.

Related

filesize(): stat failed for specific path - php

i am coding a simple doc managing script and need to get the file size and file type /file or folder/ in a table. somehow it doesn't work into the mention directory. please help if possible:
<?php
$path = "./documents";
$dh = dir($path);
while( ($file=$dh->read()) )
{
if( $file=="." || $file=="..")continue;
echo "<tr><td><a href='download.php?f=$file' title='Click to Open/Download'>$file</a></td>";
echo "<td>";
echo (is_file($file))? "<img src='file.jpg'/> FILE" : "<img src='folder.jpg'/> FOLDER ";
echo "</td><td>" .filesize($file)."</td>";
echo "<td><input type='checkbox' name='delete[]'/></td></tr>";
}
?>
it does actually has 2 errors - one the file size doesn't work for the location, if i change it to path to "." - everything is ok, but if i try to change to the folder where i need it /documents ...all goes bad, and secondly - it doesn't take the right icon file as well, same type of problem.
thank you
Problem is, $file is only the filename without the directory prefix, so checking on it won't work. One way would be to have a variable with the absolute filename (say $realfile). You'd then have to alter your code and use this variable for the file checks:
<?php
$path = "./documents";
$dh = dir($path);
while(($file=$dh->read()) !== false) {
if( $file=="." || $file=="..") continue;
// have a new variable for the real filepath
$realfile = $path . "/" . $file;
echo "<tr><td><a href='download.php?f=$file' title='Click to Open/Download'>$file</a></td>";
echo "<td>";
echo (is_file($realfile))? "<img src='file.jpg'/> FILE" : "<img src='folder.jpg'/> FOLDER ";
echo "</td><td>" .filesize($realfile)."</td>";
echo "<td><input type='checkbox' name='delete[]'/></td></tr>";
}
?>
If anyone still encounters this error and the top answer didn't work for you. Then it must be because there is a special character in your filepath i.e. \r or \n
Try:
$f = str_replace(Array("\n", "\r", "\n\r"), '', $f);
This is a common problem for reading content on a file.

unable to insert the data values using pdo in PHP

Hello I have been writing a code for uploading a file to server
and store the values on the Mysql database
I am able to upload the file but facing issues in inserting the values in MYSQL server
I'm retrieving the values from an HTML Form and I'm successfully able to get the values and was able to echo from the file
Help is needed in inserting into table part of the code
<?php
require "test.php";
$username=$_POST['username'];
$filename=$_FILES['uploadedfile']['name'];
$language=$_POST['language'];
$comment=$_POST['comment'];
$user_id=$_POST['user_id'];
$filenames=$_FILES['uploadedfile']['name'];
$category=$_POST['category'];
$subcategory=$_POST['subcategory'];
$comment=$_POST['comment'];
$language=$_POST['language'];
$duration=$_POST['duration'];
$domain ='example.com/store/upload/';
$path=$domain.$category.'/'.$filenames;
//$path=$domain.$category.'/'.$filenames;
// Where the file is going to be placed
$target_path = "upload/".$category.'/';
/* Add the original filename to our target path.
Result is "uploads/filename.extension" */
$target_path = $target_path . basename( $_FILES['uploadedfile']['name']);
if(move_uploaded_file($_FILES['uploadedfile']['tmp_name'], $target_path)) {
echo "The file ". basename( $_FILES['uploadedfile']['name']).
" has been uploaded";
} else{
echo "There was an error uploading the file, please try again!";
echo "filename: " . basename( $_FILES['uploadedfile']['name']);
echo "target_path: " .$target_path;
}
echo $filenames."<br />";
echo $domain."<br />";
echo $category."<br />";
echo $path."<br />";
echo $filename."<br />";
echo $language."<br />";
echo $comment."<br />";
echo $subcategory."<br />";
echo $duration."<br />";
echo $user_id."<br />";
// query
try{
$sql="INSERT INTO vup_file(filename,path,category,sub-category,user_id,comment,language,duration)
VALUES (:filename,:path,:category,:subcategory,:user_id,:comment,:language,:duration)";
$query=$conn->prepare($sql);
$query->execute(array(':filename'=>$filename,':path'=>$path,':category'=>$category,':subcategory'=>$subcategory,':user_id'=>$user_id,':comment'=>$comment,':language'=>$language,':duration'=>$duration));
echo 'Inserted';
}catch(PDOException $e)
{
echo 'ERROR OCCURED : '.$e->getMessage();
}
?>
Your sub-category column in your query, contains a hyphen. It needs to be escaped with backticks.
`sub-category`
Add $conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); right after the connection is opened, which would have signaled the error.
SQL is evaluating it as a mathematical problem (minus).
Which translates to "sub" minus "category".
Another option you have is to simply rename your column to sub_category with an underscore, without the need to escape it.
An insight
If by chance your column is called sub_category instead of sub-category as shown in your query/question, then you will need to change it to sub_category in your query.
Or, if it's called subcategory. Only you know what your column is called.

copy() Warning Filename cannot be empty

I'm trying to save the external images on my site
I have this code:
$file = $_GET['url'];
$newfile = $_SERVER['DOCUMENT_ROOT'] . '/images/name.jpg';
if ( copy($file, $newfile) ) {
echo "Copy saved";
}else{
echo "copy failed";
}
it works well and save images on my server OK, but when I try to use inside FOR, i have this warning:
Warning: copy(): Filename cannot be empty in /home/animesad/public_html/bot/getimg.php on line 18
My code is:
<?php
require "../functions.php";
$p = $_GET['p'];
$url = "http://www.mysite.net/en/movie/page/".$p."/";
$source = curl_get_contents("$url");
preg_match_all('#<li title="(.*?)"><a href#',$source,$name); //outputs title post
preg_match_all('#<img src="(.*?)" width="140" height="200"/>#',$source,$img); //outputs img url
for($i=0;$i<10;$i++){
//print_r($name[1][$i]."<hr />"); //that code show the names OK. (Title posts);
$namefile = slugify($name[1][$i]); //slugify is my function that convert titles in slug.
$newfile = $_SERVER['DOCUMENT_ROOT'] .'/img/'.$namefile.'.jpg';
if ( copy($file, $newfile) ) {
echo "copy saved <hr />";
}else{
echo "copy failed <hr />";
}
}
?>
Any idea is welcome.
you forgot your $file
$file = $_GET['url'];
for($i=0;$i<10;$i++){
$namefile = slugify($name[1][$i]); //slugify is my function that convert titles in slug.
$newfile = $_SERVER['DOCUMENT_ROOT'] .'/img/'.$namefile.'.jpg';
if ( copy($file, $newfile) ) {
echo "copy saved <hr />";
}else{
echo "copy failed <hr />";
}
}

php code to display images from directory not working

i have this code to display images, where each user has his own, i'll comment it to save your time
<?php
session_start();
$name=$_SESSION['valid_user']; //saved current username in variable
$loc="./uploads/"; //location of image directory
$path=$loc.$name; //current user's folder to save his images
echo $path."<br>"; //i used this to make sure the path is ok, only for testing
if(is_dir($path)) //if directory exists, show it exists,otherwise show it
{ //doesnt exists
echo "<br>exists";
}
else
{
echo "<br>not exists";
}
$files = glob($path."/");
for ($i=1; $i<count($files); $i++)
{
$num = $files[$i]; //picture number
print $num."<br />";
echo '<img src="'.$path.'" alt="random image" height="100" width="100"/>'."<br /><br />";
} //shows the picture till the last one
?>
the output that i get is this this
./uploads/user_name
exists
but it does not show the images, even though the folder is not empty (upload script works fine).
EDIT; solved it (low rep, cant answer my own question).
got it. For anyone who cares, this line here
echo '<img src="' . $path . '/' . $files[$i] . '" <!-- etc --> />';
wasn't working because i added $files, which already contained the path, and it was giving input to img src as
/uploads/username/uploads/username
so that was two times the same path.Upon removing $path, and using just
<img src="' . $files[$i] . '"
did the trick. Thank you all for your help.
I think you need to pass a wildcard path to glob: glob($path . '/*'). You are also not printing the filename in the image source attribute:
echo '<img src="' . $path . '/' . $files[$i] . '" <!-- etc --> />';
Also, your $num is actually the filename, not the picture number - that is $i. You could really simplify that loop using the foreach construct:
foreach($files as $filename) {
// etc
}
you need to add a pattern for using glob afaik
$files = glob($path."/*.*"); // all files
$files = glob($path."/*.jpg"); // all jpgs etc.pp
foreach($files as $idx => $file)
{
$num = $idx+1; //idx starts with 0 so we add one here
print $num."<br />";
echo '<img src="'.$path.'/'.$file'" alt="random image" height="100" width="100"/>'."<br /><br />";
}

path to image folder on server

i have a wordpress inside Public html folder on server.
i want to dispaly images from the folder Public_html--->Trial-->Wordpress_site-->uploads
below is page.php code
<?php
$directory = dirname(__FILE__).'/uploads';
echo $directory;
try {
// Styling for images
foreach ( new DirectoryIterator("/" . $directory) as $item ) {
if ($item->isFile()) {
echo "<div class=\"expand_image\">";
$path = "/" . $directory . "/" . $item;
echo $path;
echo "<img src=/"". $path . "\" width=861 height=443 />";
echo "</div>";
}
}
}
catch(Exception $e) {
echo 'No images found for this player.<br />';
}
?>
The images arent getting displayed..
anyone knows about the same??
edit1
I think there is problem in this sentence
echo "<img src=/"". $path . "\" width=861 height=443 />";
is it?
edit2
//home/softwar2/public_html/Pradnnya_blog/wordpress_site/wp-content/themes/deep-red/our_results/4thpanelfinal.jpg
is the path that i get when echoed.
__FILE__ gives you the path of the current file on the filesystem; however, when you visit the webpage and you see a link in the tag, you'll try to access that as a URL instead of a file. For this, you might find $_SERVER['PHP_SELF'] useful, or another one of the $_SERVER elements. It might be better to have the URL in a configuration file though, because $_SERVER may sometimes not be set.
Good catch, there's a bit of a syntax error:
echo "<img src=\"". $path . "\" width=861 height=443 />";
You'll want to use the backslash to escape the double quote.

Categories