I have a desktop application that requires images and files to be uploaded to a web server so that the data is available remotely. Install xampp and I'm using MySQL perfectly. For uploading files I have a PHP script that works correctly. I can store images without any problem.
I made this script to remove the images:
<?php
$target_path = $_POST['imagenAEliminar'];
if (unlink($_POST['imagenAEliminar'])) {
echo "[web]Se elimino correctamente.";
}else{
if (is_null($_POST['imagenAEliminar'])) {
echo "[web] Es nulo.";
}
echo "[web]Algo sucedio y no se elimino";
}
?>
I send it by post. I know the code works because with:
echo $_POST['imagenAEliminar']
It correctly prints the route that I sent from java. Still, it gives me the following error:
Warning:unlink('imagesCarrduci/image002.png'):
No such file or directory in C:\xampp\htdocs_e_l_i_m_i_n_a_r.php on line 8
The image exists and the directory is correct. if i put
unlink('imagenesCarrduci/image002.png');
Delete the image correctly.
I'm not very knowledgeable about PHP so I'm a bit confused on this. Why can this happen? How can I solve that?
UPDATE
This is the code that works perfectly to upload files:
<?php
error_reporting(0);
$target_path = "imagenes/";
$target_path = $target_path . basename( $_FILES['uploadedfile']['name']);
if(move_uploaded_file($_FILES['uploadedfile']['tmp_name'], $target_path)) {
echo "[web]El archivo ". basename( $_FILES['uploadedfile']['name']). " ha sido subido";
} else{
if (is_null($_FILES['uploadedfile']['name'])) {
echo "[web] Es nulo.";
} else{
echo "[web]Ha ocurrido un error, trate de nuevo!";
}
}
?>
In case it somehow serves as a reference or helps with the solution.
This is the code I modified by @Xerif:
<?php
// Defines una CONSTANTE con el directorio raiz + trozo de rura a la carpeta necesaria
define("RAIZ", $_SERVER['DOCUMENT_ROOT'].'/imagenes/');
chmod(RAIZ.$_POST['imagenAEliminar'], 0755);
echo RAIZ.$_POST['imagenAEliminar'];
// añades la RAIZ a unlink
if (unlink(RAIZ.$_POST['imagenAEliminar']))
{
echo "[web]Se elimino correctamente.";
}
else
{
if (is_null($_POST['imagenAEliminar']))
{
echo "[web] Es nulo.";
}
echo "[web]Algo sucedio y no se elimino";
}
This is the error it generates:
Warning: chmod(): No such file or directory in C:\xampp\htdocs_e_l_i_m_i_n_a_r.php on line 5
C:/xampp/htdocs/images/image002.png
Warning : unlink(C:/xampp/htdocs/images/image002. png): Invalid argument in C:\xampp\htdocs_e_l_i_m_i_n_a_r.php on line 9
[web]Something happened and it was not removed
My recommendation is that you use absolute paths, they give less problems:
you can do something like this
This way you will avoid headaches with relative paths.
On the other hand if you want to use relative paths it is recommended that you use the ./ (dot, slash) at the beginning to indicate the current directory.
I found the bug! The mistake was not what she thought. The error came from the java code that was implemented. Before sending the message append to the string "\r\n", escape characters. When they arrive, php does not print them but stores them in the string.
A friend reviewed the code and was able to notice this in the apache logs. Delete the line and everything works fine. That happens to me by copying and pasting but it remains from experience.
And as the saying goes:
"THE LAZY WORKS TWICE"