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
https://forums.phpfreaks.com/topic/208142-simple-question/
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
https://forums.phpfreaks.com/topic/208142-simple-question/#findComment-1088012
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
https://forums.phpfreaks.com/topic/208142-simple-question/#findComment-1088013
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
https://forums.phpfreaks.com/topic/208142-simple-question/#findComment-1088015
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.