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

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";

Related

How to copy an image from one folder to another using php

I'm having difficulty in copying an image from one folder to another, now i have seen many articles and questions regarding this, none of them makes sense or work, i have also used copy function but its giving me an error. " failed to open stream: No such file or directory" i think the copy function is only for files. The image i wanna copy is present in the root directory. Can anybody help me please. What i am doing wrong here or is there any other way???
<?php
$pic="somepic.jpg";
copy($pic,'test/Uploads');
?>
You should write your code same as below :
<?php
$imagePath = "/var/www/projectName/Images/somepic.jpg";
$newPath = "/test/Uploads/";
$ext = '.jpg';
$newName = $newPath."a".$ext;
$copied = copy($imagePath , $newName);
if ((!$copied))
{
echo "Error : Not Copied";
}
else
{
echo "Copied Successful";
}
?>
You should have file name in destination like:
copy($pic,'test/Uploads/'.$pic);
For your code, it must be like this:
$pic="somepic.jpg";
copy($pic,'test/Uploads/'.$pic);
Or use function, like this:
$pic="somepic.jpg";
copy_files($pic,'test/Uploads');
function copy_files($file_path, $dest_path){
if (strpos($file_path, '/') !== false) {
$pathinfo = pathinfo($file_path);
$dest_path = str_replace($pathinfo['dirname'], $dest_path, $file_path);
}else{
$dest_path = $dest_path.'/'.$file_path;
}
return copy($pic, $dest_path);
}

PHP file_exists returns false with existing files

I'm having a little problem with my PHP-code. I want to create a site which has different directories. Every directories has a file named pass (not .txt or something) with the value of the directories password. So if the pass-file doesn't exist the group will not exist. But even if the group exists it still says the group doesn't exist and I can't fopen the file either, but I can locate to it. Here's my code:
<?php
$name = $_POST['name'];
$group = $_POST['group'];
$pass = $_POST['pass'];
$filename = '/groups/' . $group . '/pass';
if(file_exists($filename)){
$handle = fopen($filename) or die("can't open file");
$hoi = fread($handle, filesize($filename));
fclose($handle);
if ($pass === $hoi){
session_start();
$_SESSION['name'] = $name;
header('Location: http://www.google.com');
}
else{
echo 'Password is wrong!';
}
}
else{
echo 'Group does not exist!';
}
?>
All POST-data is correct btw. Thanks for your help!
I think, that file pass must have ending. eg. .php or .html or something File name '/groups/' . $group . '/pass'; is wrong.
PHP function is named file_exist() and files must have endings!
If it is folder it has sometimes slash at the end. But it can be file. With .htaccess redirect, on index.html or index.php could be slash at the end of the file.

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.

File Exists? Display image else display default image

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;
}

Categories