PHP unlink keeps failing - php

I'm trying to write a script to upload a script to remove an image from my server. I keep getting the error message. Can anyone find anything wrong with this code?
// Delete image
if(isset($_GET['deleteImg']) && !empty($_GET['deleteImg']) && $_GET['deleteImg'] == true)
{
// Get imagepath from database
$result = mysql_query("SELECT image FROM frankkluytmans WHERE id=$id");
$imageDeletePath = mysql_fetch_assoc($result);
// Delete image from server
if(unlink($imageDeletePath['image']))
{
// Continue if image has been reset in database
if(mysql_query("UPDATE frankkluytmans SET `image`='' WHERE id=$id")){
// once deleted, redirect back to the view page
header("Location: index.php");
}
}
else
{
?>
<script type="text/javascript">
window.alert('This image could not be deleted.';
</script>
<?
}
}

I guess your error is in the path of your unlink() function, as you said actual field is like /gfx/image.png wich doesn't look to me like an absolute path, correct me if i'm wrong.
To delete the file and use unlink() directly your script should be in the same folder as the image. So i think it would be better to set an absolute path to your entry like
$path_abs = ' /customers/d/8/e/frankkluytmans.nl/httpd.www/testsite/cms'; //is the `gfx` folder inside `cms` folder? if it is this will work otherwise you have to change
if(unlink($path_abs . $imageDeletePath['image']))

Related

Reading file contents of a website?

All I want to check is that if a image exists in the link. I load it into an iframe. It was working fine but it seems they have removed the image but a blank.gif still exits.
NOTE: The link is a different domain
I tried the following codes in vain:
<?php
$varia = file_get_contents($url);
echo $varia;
echo "<pre>";
print_r(get_headers($url));
?>
and
$variablee = get_data($url);
pr($variablee);
All I get in the output is:
HTTP ERROR 404
Problem accessing
I want to put the condition that if blank.gif exits......some condition else some other condition.
What should I do?
In my opinion, the best way to test if an URI actual is an image, is to see what getimagesize returns. If it returns an array, it is an image :
function imageExists($uri) {
$info = #getimagesize($uri);
return is_array($info);
}
if (imageExists($uri)) {
...
}
Try
$content = #file_get_contents($url);
if ($content !== false) {
// FILE EXISTS
} else {
// FILE NOT EXISTS
}
?>
You can use this code:
if(getimagesize($url))
{
//image exist
}
else
{
//image not exist
}

Catch session creation error in PHP

I wanted to be able to catch the error in session creation in PHP. My primary target is to be able to know which causes the problem. Is it because we don't have wrtie access to the folder where the session is written or because we don't have free disk space already.
So of course we will start with this.
session_start();
and from this post Check if session is set or not, and if not create one? I did try to add
if(session_id() == '')
{
// session has NOT been started
session_start();
echo "Session is started";
}
else
{
// session has been started
}
And to test this I have remove writing permissions for the group and others in /tmp/ by using this command
chmod 755 tmp/
But base on my test I still see that the session is started. And the funny thing is I can login but when I try to logout I can't.
Any insights on how to properly get the reason on session creation error would be greatly appreaciated. Thanks!
EDIT:
I have tried Frederik's suggestion and done this.
try{
// Start session
session_start();
echo "Session started. " . session_id();
}catch(Exception $e){
echo "Session not started. " . $e->getMessage();
}
But I still see this
Session started. fa505cbf4b232773669008495fd08b9f
even if I have remove write permission to /tmp already. It seems that the try catch was not able to properly catch the problem.
You might want to try the is_writable function.
Something like:
<?php
$dirname = '/tmp';
if (is_writable($dirname)) {
echo 'The folder is writable';
} else {
echo 'The folder is not writable';
}
?>
You can use it with files or folders.
More information: http://id1.php.net/is_writable
With Frederik's suggestion to check if the /tmp do have write permission I was able to decide to just check for write permission and disk space before checking for username and password. So I wrote this code
if (is_writable(session_save_path())){
if ( $diskfree is <= 0){
// check for authentication here
}else{
header("Location: login.php?error=true&nodisk=true");
return;
}
}else {
header("Location: login.php?error=true&nowrite=true");
return;
}
and then in login.php I have this to catch the error code.
// If we have an error show error message
if ( isset($_GET["error"]) && $_GET["error"] ) {
//If not enough disk space show disk space message
if( isset($_GET["nodisk"]) && $_GET["nodisk"] ){
alert("disk free problem");
}
//If we don't have permission to session save path show session creation error
if( isset($_GET["nowrite"]) && $_GET["nowrite"] ){
alert("write permission error");
}

How to get HTML elements from a file using PHP

Currently I am using file_get_contents to get contents of an HTML page I have.
if(file_exists($fileName)) {
echo "file exists!";
$current = file_get_contents("./docs/page1.html");
echo $current;
} else {
echo "file doesn't exist";
file_put_content($fileName, "testing creating a new page");
}
when the HTML file exists, I store the content using file_get_contents in variable $current and then echo current. However, nothing displays. Just a black page (which I'm guessing is the css in the html page)
If I can't spit out the page, is there a way to extract each element of the html page (divs, etc)?
file_put_content is not a function. Try file_put_contents
Also look at the changes i did to your script. It will give you information if the file can be created/read or not.
<?php
$fileName = './docs/page1.html';
if($current = file_get_contents($fileName)){
echo "file exists!";
echo $current;
} else {
echo "file doesn't exist";
if (file_put_contents($fileName, "testing creating a new page")){
echo 'created a new file!';
} else {
echo 'Error, couldnt create file! Maybe I dont have write permissions?';
}
}
?>
I tested this script, and it works unless it doesnt have file permissions. So make sure you set file permissions on the folder containing the file as well.
You are showing the page, but are probably lacking content on the page that uses relative paths, such as CSS and images.
If you wish to only use part of the page, I suggest looking into DOMDocument.
http://www.php.net/manual/en/domdocument.loadhtml.php

Not able to create new directory with variable (PHP)

Hello I'm trying to create a directory in the users/ folder. When the user registers a vpb_save_details.php is being opened. And mkdir is ran to create a user with the username as directory name.
The user is being registered as normal but the problem that no directory is created, not even the /user directory.
If i run this code:
<?php
mkdir("blah", 0777);
?>
The directory bla is created, but if I run my code vpb_save_details.php, nothing happens.
This is a part of the code:
//If the user is validated and every thing is alright, then save the user details and display success message to the user
//otherwise, display error message to the user
if ($vpb_error == '')
{
//Encrypt user information with Base64 before saving them to a file
if(fwrite($vpb_database, "\r\n".base64_encode($fullname)."::::::::::".base64_encode($username)."::::::::::".base64_encode($email)."::::::::::".base64_encode($encrypted_user_password).""))
{
echo '<font style="font-size:0px;">completed</font>';
echo '<div class="info">Congrats <b>'.$fullname.'</b>, you have registered successful. <br>You may now click on the login button to log into your account.<br>Thanks.</div>';
//
// HERE! I create a folder in the users directory with the variable $username as username
//
$newUserName = $username;
$userPath = "users/".$newUserName;
mkdir("$userPath", 0777);
//copy("index.php",$userPath/index.php);
}
else
{
echo '<div class="info">Sorry, your account creation was unsuccessful, please try again (1).</div>';
}
}
else
{
echo $vpb_error;
}
fclose($vpb_database);
I have absolutely no idea what the problem could be? the code:
$newUserName = $username;
$userPath = "users/".$newUserName;
mkdir("$userPath", 0777);
Should work just fine, or am I missing a small detail?
Remove the quotes.
mkdir($userPath, 0777);

Page navigation with php

I am trying to create a dynamic website, the index.php includes the following code in the content area of the website:
<?PHP
// if undefined then define
if(!$od || $od == ""){
$od = "news";
}
// check if exists prep
$link = 'incs'."/".$od.$ext;
flush();
$fp = fopen($link, "r");
// check if inclusion file not exists then return error
if (!$fp) {
echo "An error has ocurred while processing your request. The file linked as ?od=".$od." does not appear to exist.";
}
// if exists then return parse
else {
fclose($fp);
include 'incs'."/".$od.$ext;
}
echo '</body>'."\n";
echo '</html>'."\n";
?>
I also have various links throughout the site to pages like register, login, etc.
Those links point to pages like ?od=register, ?od=login, etc.
The website will pull the default file for me, news, and display that in my content section of my website, but when I click register, the url in the address bar DOES change to /?od=register, but the default news remains in the content section, is there an error in the code above? Or am I just missing something?
P.S. $ext is defined in my config file as inc.php, which is included at the top of the index page.
Besides the fact that this is very insecure - you are making a GET request, access the variables through the $_GET array ie $od = $_GET['od']
I believe you need to define $od with a $_GET['od'] or $_REQUEST['od']
$od = $_GET['od'];
// if undefined then define
if(!$od || $od == ""){
$od = "news";

Categories