Jump to content

Simple Question


Agent

Recommended Posts

So I am just starting to learn PHP this summer but have hit a road block. It is probably something simple I am forgetting about.

 

So basically delete.php?file=123.jpg

123.jpg is in 'files/' from where the script is.

 

<?php
$deletefile = $_GET['file'];
if (file_exists(files/$deletefile)) {
unlink('files/$deletefile');
} else {
echo ('We had a problem completing your request.');
}
?>

Link to comment
Share on other sites

So I am just starting to learn PHP this summer but have hit a road block. It is probably something simple I am forgetting about.

 

So basically delete.php?file=123.jpg

123.jpg is in 'files/' from where the script is.

 

<?php
$deletefile = $_GET['file'];
if (file_exists(files/$deletefile)) {
   unlink('files/$deletefile');
} else {
   echo ('We had a problem completing your request.');
}
?>

 

I didn't see an actual question in there but I saw an issue with your code...

 

this line:

 

if (file_exists(files/$deletefile)) {

 

should be

 

if (file_exists('files/'.$deletefile)) 

 

notice the ''. ?

You must encapsulate and concatenate.

 

Edit: Same thing with the unlink function

Link to comment
Share on other sites

So I am just starting to learn PHP this summer but have hit a road block. It is probably something simple I am forgetting about.

 

So basically delete.php?file=123.jpg

123.jpg is in 'files/' from where the script is.

 

<?php
$deletefile = $_GET['file'];
if (file_exists(files/$deletefile)) {
   unlink('files/$deletefile');
} else {
   echo ('We had a problem completing your request.');
}
?>

 

I didn't see an actual question in there but I saw an issue with your code...

 

this line:

 

if (file_exists(files/$deletefile)) {

 

should be

 

if (file_exists('files/'.$deletefile))

 

notice the ''. ?

You must encapsulate and concatenate.

 

Thank you, I am still learning and guess I didn't get to that point.

Link to comment
Share on other sites

If you were doing this on a system with error_reporting set to E_ALL and display_errors set to ON in your master php.ini, all the errors that php detects will be reported and displayed. You will save a ton of time.

 

There would be a error for the problem that YourNameHere pointed out and there would be an error when the unlink() statement executes as well because you put single-quotes around the string in it and php variables are not replaced with their value when enclosed in single-quotes, but they are when enclosed in double-quotes.

Link to comment
Share on other sites

At the top of the .php page,

 

<?php
error_reporting(2);
// Should help you debug these simple errors.
?>

 

More like error_reporting(30719), or better still, the easy to read version. error_reporting(E_ALL).

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.