Not able to delete files from filesystem using php - php

I am not able to delete the files from a folder using php whenever the user clicks yes to delete form submission.
The files are still present in the folder even after using unlink() function:
<form method='post'>
<input type='submit' name='del' value='Yes'>
</form>
<?php
if(isset($_POST['del']))
{
$filename=$userid.".jpg";
unlink('upload-cover/uploads/$userid/$filename');
echo "Your image has been deleted successfully!!";
}
?>

You have to have the string passed to the unlink function in double-quotes. This is because PHP will interpret strings in single-quotes literally, therefore not including your variables. Try this:
unlink("upload-cover/uploads/$userid/$filename");
Or:
unlink("upload-cover/uploads/".$userid."/".$filename);
I think the second option is a lot more readable, and prevents errors like the one you encountered!
This is a great answer to understand PHP strings and paths:
What is the difference between single-quoted and double-quoted strings in PHP?

If you use single quotes, the file name genrated will be incorrect.
Also, make sure you have right permissions
Try with
unlink("upload-cover/uploads/$userid/$filename");

By looking into your code it seems that it is variable look up problem
variable inside single quotes are string to the php engine
unlink('upload-cover/uploads/$userid/$filename');
where as variable inside double quotes are variable to the php engine
unlink("upload-cover/uploads/$userid/$filename");

Related

The file array not getting set when uploading files having single quote in names

I am building a web application using CodeIgniter framework. The issue is that while uploading a file that have a single quote ' in the file name (Eg : Rob's.wmv) I get an empty file array in the controller. CI do have code to update the file name when the name contains special characters, but since the array is not set correctly the uploading doesn't take place. I can't understand what's going on, I tried debugging, it appears the uploading works fine on my local machine, but not on the server, this makes it even more interesting.
UPDATE The file array var_dump returns an empty array, so there's no way I can even get the file name in the script. The file array is not set, I don't get any information about the file at all.
You have to use the addslashes() from php.
The addslashes() function returns a string with backslashes in front of predefined characters.
example:
$var = "Rob's.wmv";
//then
$var = addslashes($var);
The predefined characters are:
single quote (')
double quote (")
backslash (\)
NULL
Tip: This function can be used to prepare a string for storage in a database and database queries.
As you upgrade your question:
please read this two article carefully, this is just a configuration problem. After that i think you are able to do it.
codeigniter-file-upload
how-to-upload-file-in-codeigniter

PHP onmouseover change image called from database

echo'<img src="'.$row['filename'].'" onmouseover="this.src='.$row['back_filename'].'" onmouseout="this.src='.$row['filename'].'" />';
I'm calling in 2 images from a database using mySql and php, How come this onmousover doesn't work?
ps. I'm calling a path to the image not storing the image in the database itself.
try this
echo'<img src="'.$row['filename'].'" onmouseover="this.src=\''.$row['back_filename'].'\'" onmouseout="this.src=\''.$row['filename'].'\'" />';
You are not providing the needed quotes for the inline javascript, you need single quotes '' around the filename as it is a string, causing whatever the variables hold to be interpreted by javascript as something other than what you expect.
Also use a heredoc to help with preventing errors caused by misquoting and worrying about escaping quotes.
echo <<<END
<img src="{$row['filename']}" onmouseover="this.src='{$row['back_filename']}'" onmouseout="this.src='{$row['filename']}'" />
END;

PHP echo changing text?

I'm a beginner PHP programmer, and I was wondering what was wrong with my code.
Here is the small excerpt from the affected spot:
echo "<form action='?tab=4' name='toedit5' method='get'><input value='text' onblur='edit('toedit5')' /></form>";
In Chrome's Developer Tools, the form element totally disappears, and the edit('toedit5') becomes edit(' toedit5').
The edit() function doesn't execute.
Is there anything wrong with this one line of code? Otherwise it is outside code messing with it. Sorry I didn't include it, but I don't know what to include. If you need more information, please tell me.
Thanks.
You need to escape your quotes inside your quoted echo'd statement, like this:
<?php
echo "<form action='?tab=4' name='toedit5' method='get'>";
echo "<input value='text' onblur='edit(\"toedit5\")' />"; // escaped..!
echo "</form>";
?>
It helped me to think about it like this when I was starting out: how does your browser know if the second single quote in onblur='edit('toedit5')' is closing your onblur statement or opening up the parameter? In this example, your browser will pair up the first 2 quotes it sees and assign that to the onblur attribute, i.e.: onblur='edit(' only!
Update 1:
Using the code above, I inspected a quick PHP page I created in Chrome's developer tools and was able to see the following (form available for inspection):
You really should use the more standard double quotes around the HTML properties and use single quotes around your string, with escaped single quotes within the javascript method calls. Like this:
echo '<form action="?tab=4" name="toedit5" method="get"><input value="text" onblur="edit(\'toedit5\')" /></form>';

temporarily store remote images to server

copy('https://graph.facebook.com/$fbid/picture?type=large', 'images/$fbid.jpg');
i am using the above code to store the image locally ..
the above code works without the variable. As it does not executes php in it so it is useless with links containing php variables....
The code works with a definite url is provided...
i wanna use the above url of source and destination respectively to get image...
please suggest me any other workaround or way that allows the links with variables to be executed ....
Your strings are wrapped in ' ', to use variable interpolation, you need to wrap yours strings in " ", so copy("https://graph.facebook.com/$fbid/picture?type=large", "images/$fbid.jpg"); will work.
Also, to make it clearer, it's possible to wrap your variables in { }, so "Hello {$world}" will, assuming $world contains "World", print "Hello World".
There's a few other gotchas, so have a look over the PHP manual page for strings I've put at the bottom of this post.
Ref: http://php.net/manual/en/language.types.string.php

Help in fpdf imade display

Hello every one i m using fpdf libray to creat pdf files from html form.
i m using
$pdf->Image('C:/DOCUME%7E1/mypic.PNG',60,140,120,0,'','');
to display image on pdf.
in its first parameter it asks for exact path.it doesn't accept anyaddress variable here.
but i want to make dynamic.i have able to get a complete path in an variable.
i have printed this variable.
//////////////////////////////
echo "$path";
output
////////////////////
C:/DOCUME%7E1/mypic.PNG
/////////////////////////////////////////////////////
but how i put that path from variable in this parameter.?
when i use this variable as in this function.it give error.
$pdf->Image('$path',60,140,120,0,'','');
plz help me for this.
$pdf->Image($path,60,140,120,0,'','');
This should work.
I removed the single quotes from the variable.
OR
put the $path in double quotes:-
$pdf->Image("$path",60,140,120,0,'','');
Remove the single quotes wrapping the variable name.
The single quotes make PHP treat your string as a literal (i.e. $path instead of C:/DOCUME%7E1/mypic.PNG).
Confusingly, it would work with double quotes because of variable interpolation.

Categories