PHP unlinking not working with variable - php

Going out of my mind with php unlinking
Here is my delete file script
$pictures = $_POST['data'];
//print_r ($pictures);
$imageone = $pictures[0];
$filename = "file:///Users/LUJO/Documents/CODE/REVLIVEGIT/wp-content/uploads/dropzone/" . $imageone;
echo $filename;
if (is_file($filename)) {
chmod($filename, 0777);
if (unlink($filename)) {
echo 'File deleted';
} else {
echo 'Cannot remove that file';
}
} else {
echo 'File does not exist';
}
The above does not work, error response is file does not exist
however if i change the filename path to this (the echo data from the echo above)
$filename = "file:///Users/LUJO/Documents/CODE/REVLIVEGIT/wp-content/uploads/dropzone/1420291529-whitetphoto.jpeg "
works fine and deletes the image.
Why can i not use the $imageone variable?

Do a print_r($pictures) to see if $pictures[0] is indeed the filename you're looking for.
Also note that if $pictures[0] is "//windows/*" you'll loose your windows if the user running PHP has administrative rights... so just using $pictures=$_POST["data"] is very VERY unsafe!

Related

PHP SSH move file to another directory [duplicate]

I am uploading files to a server using php and while the move_uploaded_file function returns no errors, the file is not in the destination folder. As you can see I am using the exact path from root, and the files being uploaded are lower than the max size.
$target = "/data/array1/users/ultimate/public_html/Uploads/2010/";
//Write the info to the bioHold xml file.
$xml = new DOMDocument();
$xml->load('bioHold.xml');
$xml->formatOutput = true;
$root = $xml->firstChild;
$player = $xml->createElement("player");
$image = $xml->createElement("image");
$image->setAttribute("loc", $target.basename($_FILES['image']['name']));
$player->appendChild($image);
$name = $xml->createElement("name", $_POST['name']);
$player->appendChild($name);
$number = $xml->createElement("number", $_POST['number']);
$player->appendChild($number);
$ghettoYear = $xml->createElement("ghettoYear", $_POST['ghetto']);
$player->appendChild($ghettoYear);
$schoolYear = $xml->createElement("schoolYear", $_POST['school']);
$player->appendChild($schoolYear);
$bio = $xml->createElement("bio", $_POST['bio']);
$player->appendChild($bio);
$root->appendChild($player);
$xml->save("bioHold.xml");
//Save the image to the server.
$target = $target.basename($_FILES['image']['name']);
if(is_uploaded_file($_FILES['image']['tmp_name']))
echo 'It is a file <br />';
if(!(move_uploaded_file($_FILES['image']['tmp_name'], $target))) {
echo $_FILES['image']['error']."<br />";
}
else {
echo $_FILES['image']['error']."<br />";
echo $target;
}
Any help is appreciated.
Eric R.
Most like this is a permissions issue. I'm going to assume you don't have any kind of direct shell access to check this stuff directly, so here's how to do it from within the script:
Check if the $target directory exists:
$target = '/data/etc....';
if (!is_dir($target)) {
die("Directory $target is not a directory");
}
Check if it's writeable:
if (!is_writable($target)) {
die("Directory $target is not writeable");
}
Check if the full target filename exists/is writable - maybe it exists but can't be overwritten:
$target = $target . basename($_FILES['image']['name']);
if (!is_writeable($target)) {
die("File $target isn't writeable");
}
Beyond that:
if(!(move_uploaded_file($_FILES['image']['tmp_name'], $target))) {
echo $_FILES['image']['error']."<br />";
}
Echoing out the error parameter here is of no use, it refers purely to the upload process. If the file was uploaded correctly, but could not be moved, this will still only echo out a 0 (e.g. the UPLOAD_ERR_OK constant). The proper way of checking for errors goes something like this:
if ($_FILES['images']['error'] === UPLOAD_ERR_OK) {
// file was properly uploaded
if (!is_uploaded_File(...)) {
die("Something done goofed - not uploaded file");
}
if (!move_uploaded_file(...)) {
echo "Couldn't move file, possible diagnostic information:"
print_r(error_get_last());
die();
}
} else {
die("Upload failed with error {$_FILES['images']['error']}");
}
You need to make sure that whoever is hosting your pages has the settings configured to allow you to upload and move files. Most will disable these functions as it's a sercurity risk.
Just email them and ask whether they are enabled.
Hope this helps.
your calls to is_uploaded_file and move_uploaded_file vary. for is_uploaded_file you are checking the 'name' and for move_uploaded_file you are passing in 'tmp_name'. try changing your call to move_uploaded_file to use 'name'

PHP unlink removes file, but file still exists

I have a very simple function:
unlink($oldPicture);
if (is_readable($oldPicture)) {
echo 'The file is readable';
} else {
echo 'The file is not readable';
}
}
The file shows not readable after execution and disappears from the file directory. However, it's still available when accessed from the browser despite not being cached (opened the file on separate browsers for testing). Is there something I am missing here? Is the file being cached by the server? That is the only explanation I could come up with.
Try something like:
if (is_file($oldPicture)) {
chmod($oldPicture, 0777);
if (unlink($oldPicture)) {
echo 'File deleted';
} else {
echo 'Can\'t remove file';
}
} else {
echo 'File does not exist';
}
Make sure you have full path for $oldPicture
Example:
$oldPicture = dirname(__FILE__) . '/oldpicture.png';

How do I know if my image exists on the server? (PHP)

I have a simple code that checks whether a file exists or not in the server.
<?php
$filename = 'www.testserver/upload/productimg/IN.ZL.6L_sml.jpg';
if (file_exists($filename)) {
echo "The file $filename exists";
} else {
echo "The file $filename does not exist";
}
?>
Problem: My local webserver says that the file does not exist even though when I copy paste the url in the browser, I can see the image.
How can I make the condition say that the file does not really exist?
Another option would be, making a request and checking for the response headers:
<?php
$headers = get_headers('http://www.example.com/file.jpg');
if($headers[0]=='HTTP/1.0 200 OK'){
return true; // file exists
}else{
return false; // file doesn't exists
}
?>
Code live example:
http://codepad.viper-7.com/qfnvme
for real path of image you can try:
<?php
$filename = 'www.testserver/upload/productimg/IN.ZL.6L_sml.jpg';
$testImage = #file_get_contents($filename);
if ($testImage != NULL) {
echo "The file $filename exists";
} else {
echo "The file $filename does not exist";
}
?>
Use
$filename = $_SERVER["DOCUMENT_ROOT"]."/upload/productimg/IN.ZL.6L_sml.jpg";
for testing the file.

deleting image from a file in php

I am trying to delete a file from folder in php here is my model function
function deleteFiles()
{
$file = "http://localhost/copycopy/img/uploaded/long.jpeg";
if(is_file($file))
{
#unlink($file); // delete file
echo $file.'file deleted';
}
else
{
echo "no file";
}
}
but I always see "no file" and file is never deleted, file is in folder,because the url in $file actually displays the file in browser
help me
instead of using web url
$file = "http://localhost/copycopy/img/uploaded/long.jpeg";
use local path to file:
$file = $pathToYourWebSite . "/copycopy/img/uploaded/long.jpeg";
be sure to set $pathToYourWebSite to real location of your website;

copy images issue in php

I want to copy some images from one folder to another, I do not want to copy them all.
Here is my code but it give me an issue failed to open stream
$image = "a.jpg";
$srcfile='uploads/listings/my_from/'.$image;
$dstfile='uploads/listings/my_to/images/';
copy($srcfile, $dstfile);
What am I missing? Can I do anyhting else so that I can copy selected images to destination without deleting it?
Note: both of these folders are on the same server and same project. Should I do it by curl?
It might pay to do some error checking as well, but you just need to add the full image path to your $dstfile variable:
<?php
$image = "a.jpg";
$srcfile='uploads/listings/my_from/'.$image;
$dstfile='uploads/listings/my_to/images/'.$image;
echo 'Attempting to copy "'.$srcfile.'" to "'.$dstfile.'"<br />';
if(file_exists($srcfile)) {
if(file_exists($dstfile)) {
echo 'Cannot copy file, destination file already exists';
} else {
if(is_writable(dirname($dstfile))) {
if(copy($srcfile,$dstfile)) {
echo 'File successfully copied';
} else {
echo 'File could not be copied';
}
} else {
echo 'Destination is not writable';
}
}
} else {
echo 'Cannot copy file, source file doesnt exist';
}
?>
if you are sure that file a.jpg exists, then this should work
$srcfile='uploads/listings/my_from/'.$image;
$dstfile='uploads/listings/my_to/images/'.$image;
copy($srcfile, $dstfile);
note: destination file name must be specified.
You should check it exists really.
try:
$image = "a.jpg";
$srcfile='uploads/listings/my_from/'. $image;
$dstfile='uploads/listings/my_to/images/' . $image;
if(!file_exists($srcfile) {
throw new \Exception("File does not exist!");
}
copy($srcfile, $dstfile);

Categories