move file that is uploaded to another pc/ ip address - php

I just wanna ask if its possible to make directory and store the uploaded file in it to a shared folder from another pc using mkdir and move_uploaded_file. I tried doing it locally it works fine but when i try this code
if($_REQUEST['submit']){
$app_name = $_POST['name'];
$desc = $_POST['description'];
$file_url = "192.168.3.34/testupload/files/$app_name/" . $_FILES["file"]["name"];
$photo_loc = "192.168.3.34/testupload/images/$app_name/" . $_FILES["photo"]["name"];
$select = $_POST['select'];
$date = date('m/d/y h:i:s A');
mkdir("192.168.3.34/testupload/files/$app_name/");
mkdir("192.168.3.34/testupload/images/$app_name/");
/* some codes here that is working */
if($file != "" ){
if (file_exists("..192.168.3.34/testupload/files/$app_name/" . $_FILES["file"]["name"]) && file_exists("..192.168.3.34/testupload/images/$app_name/" . $_FILES["photo"]["name"])) {
die("File Exists");
}
move_uploaded_file($_FILES["file"]["tmp_name"],
"192.168.3.34/testupload/files/$app_name/" . $_FILES["file"]["name"]);
if($photo_url != "") {
move_uploaded_file($_FILES["photo"]["tmp_name"],
"192.168.3.34/testupload/images/$app_name/" . $_FILES["photo"]["name"]);
}
$name = escape($_REQUEST['name']);
$description = escape($_REQUEST['description']);
//$query = "INSERT into files (name, description,file_name,photo_url,date_uploaded) values ('{$name}', '{$description}','{$file}', '{$photo_url}', now() )";
//mysql_query($query) or die("Error in Query" . mysql_error());
//commit();
//redirect("manage.php");
$file_loc = "192.168.3.34/testupload/files/$app_name/" . $_FILES["file"]["name"];
$photo_loc = "192.168.3.34/testupload/images/$app_name/" . $_FILES["photo"]["name"];
}
}else{
set_error("File not Found");
//redirect('manage.php');
}
i tried to change the "/" in "\" and still getting this error
Warning: mkdir() [function.mkdir]: No such file or directory in
C:\xampp\htdocs\atts_update\upload.php on line 12
Warning: mkdir() [function.mkdir]: No such file or directory in
C:\xampp\htdocs\atts_update\upload.php on line 13
Warning:
move_uploaded_file(192.168.3.34/testupload/files/1111111111111/sss.txt)
[function.move-uploaded-file]: failed to open stream: No such file or
directory in C:\xampp\htdocs\atts_update\upload.php on line 71
Warning: move_uploaded_file() [function.move-uploaded-file]: Unable to
move 'C:\xampp\tmp\php610B.tmp' to
'192.168.3.34/testupload/files/1111111111111/sss.txt' in
C:\xampp\htdocs\atts_update\upload.php on line 71
Warning:
move_uploaded_file(192.168.3.34/testupload/images/1111111111111/Picture-in-a-picture4aaa.jpg) [function.move-uploaded-file]: failed to open stream: No such file or
directory in C:\xampp\htdocs\atts_update\upload.php on line 76
Warning: move_uploaded_file() [function.move-uploaded-file]: Unable to
move 'C:\xampp\tmp\php611C.tmp' to
'192.168.3.34/testupload/images/1111111111111/Picture-in-a-picture4aaa.jpg' in C:\xampp\htdocs\atts_update\upload.php on line 76

Related

ZipArchive ExtractTo() Permission Denied

I'm trying to extract zip files from a folder into a subfolder. The error I'm recieving is:
Warning: ZipArchive::extractTo(XXXX): failed to open stream: Permission denied in /var/www/html/update_alerts2.php on line 97
extracted
Code :
public function extractFiles($inputDir,$outputDir) {
$files_to_extract = $this->getFiles($inputDir);
$zip = new ZipArchive();
foreach ($files_to_extract as $file) {
echo $file;
$res = $zip->open($inputDir . $file);
if ($res) {
$zip->extractTo($outputDir);
$zip->deleteName($inputDir . $file);
$zip->close();
} else {
echo 'failed, code:' . $res;
}
}
}
I've tried setting permissions to 777 but I still get the error. What are the possible ways I can fix this issue (I'm using centos7 and apache) ?

Warning: move_uploaded_file(): Unable to move 'file' to 'folder'

I try to upload an image to a directory on localhost.
Here is the part in my php file, which causes the error:
if (move_uploaded_file($tmp_name, "/Users/sandro/Documents/bildgalerie/uploads")) {
echo "The file " . basename($filename) . " has been uploaded";
} else {
echo "error";
}
If I load the page, I get the following:
Warning: move_uploaded_file(): Unable to move '/private/var/folders/np/sln14hvn3tsbzc4kpjjtp17c0000gn/T/phppJbFJK' to '/Users/sandro/Documents/bildgalerie/uploads' in /Users/sandro/Documents/bildgalerie/control/upload.php on line 31

PHP move_uploaded_file

I am trying to upload files to a server using PHP move_uploaded_file and I am getting the following error:
Warning: move_uploaded_file() [function.move-uploaded-file]: The second argument to copy() function cannot be a directory in /Users/Rick/Sites/upload/upload.php on line 7
Warning: move_uploaded_file() [function.move-uploaded-file]: Unable to move '/Applications/XAMPP/xamppfiles/temp/phpDlCZUd' to '/Users/Rick/Sites/upload/uploads/richardgregson' in /Users/Rick/Sites/upload/upload.php on line 7
Below is my code, nothing complicated.
if($_POST["upload"]){
$target_path = "/Users/Rick/Sites/upload/uploads/" . $_POST["name"];
if(move_uploaded_file($_FILES['uploadedfile']['tmp_name'], $target_path)){
echo "<div class='success'>The file " . "<span class='filename'>" . basename( $_FILES['uploadedfile']['name']) . "</span>" . " has been uploaded</div>";
} else {
echo "<div class='error'>There was an error uploading the file, please try again!</div>";
}
}
The permissions on the folder it is writing to are correct. I dont understand the error "cannot be a directory as the argument is where we are moving the file to so it has to be a directory.."
Thanks
Rick
The error is telling you what's wrong - $target_path (/Users/Rick/Sites/upload/uploads/richardgregson) is a directory
Try adding an extension.
$extension=end(explode('.', $_FILES["uploadedfile"]["name"]));
$target_path = "/Users/Rick/Sites/upload/uploads/" . $_POST["name"] . '.' . $extension;
Or perhaps your putting the file into a folder
$target_path = "/Users/Rick/Sites/upload/uploads/" . $_POST["name"] . '/' . $_FILES["uploadedfile"]["name"];
Note: you should not trust the user to upload only the files you want and you should sanitise $_POST["name"], all too easy to make that '../../' and post a PHP file.

PHP recursively removing folders

Here is function that I'm using for recursively deleting folders and files
function rmdir_recursively($dir) {
if (!is_dir($dir) || is_link($dir)) return unlink($dir);
foreach (scandir($dir) as $file) {
if ($file == '.' || $file == '..') continue;
if (!rmdir_recursively($dir . DIRECTORY_SEPARATOR . $file)) {
chmod($dir . DIRECTORY_SEPARATOR . $file, 0777);
if (!rmdir_recursively($dir . DIRECTORY_SEPARATOR . $file)) return false;
};
}
return rmdir($dir);
}
The problem is, when I send some folder inside root, it deletes this folder. But when I send root folder itself like that
rmdir_recursively("./");
It returns bunch of errors like below
PHP Warning: unlink(.//wp/wp-admin/network) [<a href='function.unlink'>function.unlink</a>]: No such file or directory in /home/vefa/public_html/deploy.php on line 52
[07-Oct-2012 02:16:09] PHP Warning: unlink(.//wp/wp-admin/user) [<a href='function.unlink'>function.unlink</a>]: No such file or directory in /home/vefa/public_html/deploy.php on line 52
[07-Oct-2012 02:16:09] PHP Warning: unlink(.//wp/wp-content) [<a href='function.unlink'>function.unlink</a>]: No such file or directory in /home/vefa/public_html/deploy.php on line 52
[07-Oct-2012 02:16:09] PHP Warning: unlink(.//wp/wp-content/plugins) [<a href='function.unlink'>function.unlink</a>]: No such file or directory in /home/vefa/public_html/deploy.php on line 52
[07-Oct-2012 02:16:09] PHP Warning: unlink(.//wp/wp-content/plugins/akismet) [<a href='function.unlink'>function.unlink</a>]: No such file or directory in /home/vefa/public_html/deploy.php on line 52
What am I missing?
You say you're sending it rmdir_recursively("./");?
But look at the code, you are later calling rmdir_recursively($dir . DIRECTORY_SEPARATOR . $file).
This means that you'll be trying to eliminate files in ".//".
Which is why your error message contains unlink(.//wp/wp-admin/user)
Use rmdir_recursively("."); instead.

File upload change name gives error

$ref_id = mysql_insert_id();
$locatie = 'iso_photo/afwijkingen';
if($_FILES['uploaded']['type'] != 'application/octet-stream') // Geen php files
{
$folder = $locatie.basename($_FILES['uploaded'][$ref_id.'.jpg']) ;
move_uploaded_file($_FILES['uploaded']['tmp_name'], $folder);
}
Gives:
Warning: move_uploaded_file(iso_photo/afwijkingen) [function.move-uploaded-file]: failed to open stream: Is a directory
Warning: move_uploaded_file() [function.move-uploaded-file]: Unable to move '/tmp/php6LQCqY' to 'iso_photo/afwijkingen'
What might be wrong?
edited 4th line, should work now
$locatie = 'iso_photo/afwijkingen';
if($_FILES['uploaded']['type'] != 'application/octet-stream') // Geen php files
{
$folder = $locatie . "/" . basename($_FILES['uploaded'][$ref_id]) ;
move_uploaded_file($_FILES['uploaded']['tmp_name'], $folder);
}
but can't properly say, as i don't know content of $ref_id variable

Categories