PHP-File upload failed to open stream - wrong folder - php

I got a problem with fileuploading images.
When I try to upload I get this error:
Warning: copy(/assets/img/products/): failed to open stream: No such file or directory in /customers/d/7/4/(website name)/httpd.www/newSite/pages/admin/adminPages/products.php on line 212
This is the code it's referring to:
if(isset($_POST['submitMoreImg'])){
$name = $_POST['imgName'];
$prod_id = $_POST['prod_id'];
if(!empty($_FILES['image']))
{
$path = "/assets/img/products/";
$path = $path.basename( $_FILES['image']['imgName']);
if(copy($_FILES['image']['tmp_name'], $path)) {
echo'<script type="text/javascript">
alert("uploaded");
</script>';
uploadExtraImg($name, $prod_id);
}
else{
echo "Error: ".$sql."<br>".$connection->error;
}
}
}
I can't seem to find the correct folder, I tried a lot of different folder path.

The issue is apparent.
Warning: copy(/assets/img/products/): failed to open stream: No such file or directory in /customers/d/7/4/(website name)/httpd.www/newSite/pages/admin/adminPages/products.php on line 212
It's looking in directory /customers/d/7/4/(website name)/httpd.www/newSite/pages/admin/adminPages/products.php
But I'm hoping that you want to get /assets/img/products/customers/d/7/4
or as a complete path (website name)/httpd.www/newSite/assets/img/products/customers/d/7/4
In that case. Please make sure your path is correct.
try changing your $path value. Like so
<?php
if(isset($_POST['submitMoreImg'])){
$name = $_POST['imgName'];
$prod_id = $_POST['prod_id'];
if(!empty($_FILES['image']))
{
$path = __DIR__."/../../../assets/img/products/";
$path = $path.basename( $_FILES['image']['imgName']) + $fileNameWithExtension;
if(copy($_FILES['image']['tmp_name'], $path)) {
echo'<script type="text/javascript">
alert("uploaded");
</script>';
uploadExtraImg($name, $prod_id);
}
else{
echo "Error: ".$sql."<br>".$connection->error;
}
}
}
?>

Related

File_get_contents(): failed to open stream: Connection refused

I am trying to download a previously uploaded file, form database and uploads folder in php codeigniter. I am using code below but downloading empty file.
controller.php
public function downloadFile($incidents_id)
{
$file = $this->incidents_model->getfile($incidents_id);
//echo $file; die;
$this->load->helper('download');
$path = file_get_contents(base_url()."uploads/".$file); //(the error shows on this line)
// echo $path; die;
$name = $file; // new name for your file
// echo $name ; die;
force_download($name, $path); // start download`
}
incidents_model.php
function getfile($Incident_id )
{
$file = $this->db->select('file');
$this->db->from('incidents');
$this->db->where('incidents_id' , $Incident_id );
$query = $this->db->get();
// return $query->row();
if ($query->num_rows() > 0) {
return $query->row()->file;
}
return false;
}
view.php
<div class="form-group col-md-4">
Download file
</div>
so running this code download an empty file.
echo $file; die; displays the file name which been saved in db and in uploads folder
echo $path; die; generates an error:
Severity: Warning
Message:
file_get_contents(http://localhost:8080/ticketing_tool_v2/uploads/Screenshot
2021-03-04 at 5.59.38 PM.png): failed to open stream: Connection
refused
Filename: admin/Incidents.php
Line Number: 380
Reviewing the documentation for file_get_contents you'll observe there's many different ways you can use it. For your purposes you would need to allow inbound connections to the filesystem.
The other way you could do this for better future proofing is to use the CodeIgniter file helper - https://codeigniter.com/userguide3/helpers/file_helper.html
Before reading the file from path, please check whether a path points to a valid file.
public function downloadFile($incidents_id) {
try {
$this->load->helper('download');
$file = $this->incidents_model->getfile($incidents_id);
$data = file_get_contents(base_url("uploads/" . $file));
$name = $file; // new name for your file
force_download($name, $data); // start download`
} catch (Exception $e) {
//exception handling code goes here
print_r($e);
}
}

PHP's move_uploaded_file error: Permission denied on Mac

I'm new to PHP and I seem to have a problem uploading file to the web server. I've made myself a simple form and have a PHP file to control the uploading but every time I run the code, I get an error. Here is the code:
<?php
$name = $_FILES['upload']['name'];
$temp = $_FILES['upload']['tmp_name'];
$error = $_FILES['upload']['error'];
if ($error = 0) {
move_uploaded_file($temp, "images/" . $name);
echo 'Success';
} else {
die ('$error');
}
?>
and this is the error:
Warning: move_uploaded_file(images/macbook.jpg): failed to open stream: No such file or directory in /Library/WebServer/Documents/doc/ch07/upload_check.php on line 7
Warning: move_uploaded_file(): Unable to move '/private/var/tmp/phpuMC0td' to 'images/macbook.jpg' in /Library/WebServer/Documents/doc/ch07/upload_check.php on line 7
Thanks in advance!
error assign vs evaluate
if ($error = 0) should be if ($error == 0)
name is from basename. also declare the target before.
$target_path_file = 'images/' .basename($_FILES['upload']['name']);
move_uploaded_file($temp, $target_path_file);

cannot move uploaded files failed to open stream

Hello so i have a simple upload system in php and i want to upload my files to ftp server but when i try to it doesnt work i get these two errors:
Warning: move_uploaded_file(/userfiles/grega): failed to open stream: No such file or directory in /srv/disk3/1618233/www/netdisk.co.nf/upload.php on line 19
Warning: move_uploaded_file(): Unable to move '/tmp/phpVtApVM' to '/userfiles/grega' in /srv/disk3/1618233/www/netdisk.co.nf/upload.php on line 19
and there is folder userfiles/grega on the ftp server please help me out
the code:
<?php
require_once 'core/init.php';
if($_POST[submit]) {
$name = $_FILES['upload']['name'];
$temp = $_FILES['upload']['tmp_name'];
$type = $_FILES['upload']['type'];
$size = $_FILES['upload']['size'];
if($size <= 5000000){
$user = new User();
if(!$user->isLoggedIn()) {
Redirect::to('index.php');
}
$uploads_dir = '/userfiles';
$username = ($user->data()->username);
move_uploaded_file($temp,"$uploads_dir/$username");
Session::flash('home', '<h3>Datoteka je bila naložena!</h3>');
Redirect::to('mojprofil.php');
} else{
echo "Napaka!";
}
} else {
header("Location: mojprofil.php");
}
?>
You say this:
there is folder userfiles/grega
But the error says this:
move_uploaded_file(/userfiles/grega)
Those are two very (even if subtly) different paths. Note also where you define the path in your code:
$uploads_dir = '/userfiles';
The code is looking for a folder called userfiles in the root of the entire file system, not just in the website. Perhaps you meant to do this?:
$uploads_dir = 'userfiles';

File upload issue in host server

When try to upload a image file, hosting server makes warning sometimes. If i try uploading the same file in succeeding tries the image will be uploaded.
Warning:
Warning (2): move_uploaded_file(files/photos/3.jpg) [http://php.net/function.move-uploaded-file]: failed to open stream: Permission denied [APP/controllers/components/file_upload.php, line 55]
Warning (2): move_uploaded_file() [http://php.net/function.move-uploaded-file]: Unable to move '/tmp/phpUutr9Z' to 'files/photos/3.jpg' [APP/controllers/components/file_upload.php, line 55]
File Upload
if ( is_uploaded_file($file_temp) ) {
if (move_uploaded_file($file_temp, $destination . $filename . '.' . $extension)) {
$error = "SUCCESS";
return $error;
} else {
$error = "ERROR";
return $error;
}
}
Here when warning comes 'ERROR' in else portion is also returned with warning...
How I can correct it ?
It is working nice in local server...
Probably your script do not have permission to write to destination directory.
Check the CHMOD on the directory and on the file. Usually PHP is ran like root, but you can still forbid it's access with CHMOD.
So is APP/controllers/components/files/photos/3.jpg where you want to save your uploads? If so then its a permissions problem.
Or do you mean: WEB_ROOT/files/photos/3.jpg if so you need to change your code too:
if ( is_uploaded_file($file_temp) ) {
if (move_uploaded_file($file_temp, '/'.$destination.$filename.'.'.$extension)) {
$error = "SUCCESS";
return $error;
} else {
$error = "ERROR";
return $error;
}
}
notice the forward slash '/'.$destination

Php scandir() problems

I was wondering, my Scandir() function works on a php $_GET variable, so the variable returns the folder, but I'm having a problem because I'm not sure how to echo out an error if there is a problem with with directory.
this is the error I am getting:
Warning: scandir(users/ro/f) [function.scandir]: failed to open dir: No such file or directory in C:\xampp\htdocs\OSO\desktop\main_content\file.php on line 31
This is my code
$folder = $_GET['file_folder'];
$directory = "users/$username/$folder";
if (scandir($directory, 0)) {
unset($documents[0], $documents[1]);
$documents = scandir($directory, 0);
// for each loop
} else {
echo "No such directory";
}
Cheers in advance
I would first check whether $directory exists using is_dir() before calling scandir():
if (is_dir($directory)) {
$filenames = scandir($directory, 0);
// do something
} else {
echo "No such directory";
}

Categories