File Exists? Display image else display default image - php

I can't seem to get the following code to work?
$playerfileLocation = "../wp-content/gallery/playerphotos/' . $playerid . '.jpg";
if(file_exits($playerfileLocation))
echo "The file File exists";
else
echo "The file File does not exist";
The "playerid" field is a number that gets passed through.
I just can't seem to get it working. Urgh!

You have mismatch of quotes. Try this code.
$playerfileLocation = "../wp-content/gallery/playerphotos/" . $playerid . ".jpg";
if(file_exists($playerfileLocation))
echo "The file File exists";
else
echo "The file File does not exist";
Update:
Actually I would suggest using below code as whenever PHP see double-quotes, it tries to parse anything in between it, which is not required in this case. This is a small optimization in performance.
$playerfileLocation = '../wp-content/gallery/playerphotos/' . $playerid . '.jpg';
if(file_exists($playerfileLocation))
echo "The file File exists";
else
echo "The file File does not exist";
Also, to just check if file exists and if not display default image use the following code:
$playerfileLocation = '../wp-content/gallery/playerphotos/' . $playerid . '.jpg';
$defaultfileLocation = '../wp-content/gallery/playerphotos/default.jpg';
if (!file_exists($playerfileLocation)) {
$playerfileLocation = $defaultfileLocation;
}

Your code is misspelled: (exist not exits)
$playerfileLocation = "../wp-content/gallery/playerphotos/$playerid.jpg";
$default_player = "../wp-content/gallery/playerphotos/default.jpg";
if(file_exits($playerfileLocation))
{
}
else
{
$playerfileLocation=$default_player;
}

In php, we can get variable value in double quotes also. It's good practice to use double quotes in path, so no much indentation is required.
$playerfileLocation = "../wp-content/gallery/playerphotos/$playerid.jpg";
$default_player = "../wp-content/gallery/playerphotos/default.jpg";
if(!file_exists($playerfileLocation))
{
$playerfileLocation=$default_player;
}

Related

Can i use content of $_SESSION[' '] to open a file?

in this case file successfully open
$friend_id=$_POST['cc'];
$extension='txt';
$file_name="$friend_id.$extension";
$handle1=fopen($file_name,'a');
Is there any way to open a file like this? codes are below
In this case unsuccessful to open file
$user=$_SESSION['$username'];
$extension='txt';
$inbox_file_name="$user.$extension";
$fh1=fopen($inbox_file_name,'a');
The better way would be:
$user=$_SESSION['$username'];
$ext ='.txt';
$file = $user.$ext;
$handle=fopen($file,'a');
This code might be helpful ie, avoid ' in $username
$user=$_SESSION[$username];
$user = trim($user);
$extension='txt';
$inbox_file_name=$user.".".$extension;
if(file_exists($inbox_file_name))
$fh1=fopen($inbox_file_name,'a');
else
echo "file not exists";
this one works perfectly
$user=$_SESSION['$username'];
$user = trim($user);
$extension='txt';
$inbox_file_name=$user.".".$extension;
if(file_exists($inbox_file_name))
$fh1=fopen($inbox_file_name,'a');
else
echo "file not exists";

PHP unlinking not working with variable

Going out of my mind with php unlinking
Here is my delete file script
$pictures = $_POST['data'];
//print_r ($pictures);
$imageone = $pictures[0];
$filename = "file:///Users/LUJO/Documents/CODE/REVLIVEGIT/wp-content/uploads/dropzone/" . $imageone;
echo $filename;
if (is_file($filename)) {
chmod($filename, 0777);
if (unlink($filename)) {
echo 'File deleted';
} else {
echo 'Cannot remove that file';
}
} else {
echo 'File does not exist';
}
The above does not work, error response is file does not exist
however if i change the filename path to this (the echo data from the echo above)
$filename = "file:///Users/LUJO/Documents/CODE/REVLIVEGIT/wp-content/uploads/dropzone/1420291529-whitetphoto.jpeg "
works fine and deletes the image.
Why can i not use the $imageone variable?
Do a print_r($pictures) to see if $pictures[0] is indeed the filename you're looking for.
Also note that if $pictures[0] is "//windows/*" you'll loose your windows if the user running PHP has administrative rights... so just using $pictures=$_POST["data"] is very VERY unsafe!

How do I know if my image exists on the server? (PHP)

I have a simple code that checks whether a file exists or not in the server.
<?php
$filename = 'www.testserver/upload/productimg/IN.ZL.6L_sml.jpg';
if (file_exists($filename)) {
echo "The file $filename exists";
} else {
echo "The file $filename does not exist";
}
?>
Problem: My local webserver says that the file does not exist even though when I copy paste the url in the browser, I can see the image.
How can I make the condition say that the file does not really exist?
Another option would be, making a request and checking for the response headers:
<?php
$headers = get_headers('http://www.example.com/file.jpg');
if($headers[0]=='HTTP/1.0 200 OK'){
return true; // file exists
}else{
return false; // file doesn't exists
}
?>
Code live example:
http://codepad.viper-7.com/qfnvme
for real path of image you can try:
<?php
$filename = 'www.testserver/upload/productimg/IN.ZL.6L_sml.jpg';
$testImage = #file_get_contents($filename);
if ($testImage != NULL) {
echo "The file $filename exists";
} else {
echo "The file $filename does not exist";
}
?>
Use
$filename = $_SERVER["DOCUMENT_ROOT"]."/upload/productimg/IN.ZL.6L_sml.jpg";
for testing the file.

upload succeeding but picture is not there

I have a php page that is supposed to store the uploaded image to my server. When I run this, I get the "Upload successful" message, but the picture has not been uploaded.
What could it be?
update: can people please leave a comment as to why they down vote my question. I'm new here and I dont know why this question got down voted. thanks
<?
if(!empty($_FILES['uploaded_file'])) {
if ($_FILES['uploaded_file']['error'] > 0 )
echo "Error: " . $_FILES['uploaded_file']['error'] . "<br />";
else{
// Add the original filename to target path.
$target_path = 'MemberPics\\user'.$userid.'.jpg' ;
$success = move_uploaded_file($_FILES['uploaded_file']['tmp_name'], $target_path);
if(!$success) {
echo "There was an error uploading the file, please try again!";
}else {
echo "Upload successful, please go back to your home page";
}
}
}
?>
I believe the problem you are running into is that you are saving the image in an incorrect location (An invalid one from the looks of your link syntax).
Either of these should work:
$target_path = 'MemberPics/user'.$userid.'.jpg' ;
or
move_uploaded_file($_FILES["uploaded_file"]["tmp_name"], "MemberPics/user" . $_FILES["uploaded_file"]["name"]);

PHP simple file upload

I'm doing a simple file upload using the following script:
$errors = '';
$target_path = "[PATH HERE]";
$target_path = $target_path . basename($_FILES['uploadFile']['name']);
if(move_uploaded_file($_FILES['uploadFile']['tmp_name'], $target_path)) {
$errors = "The file ". basename( $_FILES['uploadFile']['name']). " has been uploaded";
} else{
$errors = "There was an error uploading the file, please try again! Type: " . $_FILES['uploadFile']['type'];
}
For some reason, I get an error uploading the file and the file type is not displayed. It seems to only grab the name of the file without the extension (i.e. "test" rather than "test.pdf"). I'm sure it's something simple, but what am I doing wrong?
If you check the error element in the files array, you'll probably find it's some value other than 0. The error should be 0 if nothing went wrong. Otherwise, compare the value stored in error against the PHP documentation to determine what went wrong.
Perhaps your entering the path wrong (ending slash), or php dont have permission to write to the directory.
<?php
error_reporting(E_ALL); // Show some errors
$target_path = "/var/www/somesite.com/uploads/"; // Would require a ending slash
$target_path = $target_path.basename($_FILES['uploadFile']['name']);
if(move_uploaded_file($_FILES['uploadFile']['tmp_name'], $target_path)) {
$errors = "The file ". basename( $_FILES['uploadFile']['name']). " has been uploaded";
} else{
$errors = "There was an error uploading the file, please try again! Type: " . $_FILES['uploadFile']['type'];
}
?>

Categories