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);
Related
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;
}
}
}
?>
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';
Hello there I receive following error while try to upload file using php mailer.
move_uploaded_file(C:/inetpub/vhosts/yoursite.com/httpdocs/userfiles/resumes/5007story1.pdf) [function.move-uploaded-file]: failed to open stream: No such file or directory in D:\yoursite\wwwroot\jobseekers.php on line 87
global $imagesname;
global $image_type;
$uid = rand(100,9999);
if(isset($_FILES["img"]["name"]))
{
$image_tempname= $_FILES['img']['name'];
$imgdir ="C:/inetpub/vhosts/yoursite.com/httpdocs/";// $_SERVER ["DOCUMENT_ROOT"];
//imgdir =$_SERVER ["DOCUMENT_ROOT"];
$imgname = $imgdir. 'userfiles/resumes/'.$uid.$_FILES['img']['name'];
if($imagesname != '')
{
$imagesname=$imagesname.','.$imgname;
$image_type=$image_type.','.$_FILES['img']['type'];
}
else
{
$imagesname=$imagesname.$imgname;
$image_type=$image_type.$_FILES['img']['type'];
}
$imgurl = 'userfiles/resumes/'.$image_tempname;
//print_r($_SERVER['PATH_TRANSLATED']);
move_uploaded_file ($_FILES['img']['tmp_name'], $imgname);
}
if(isset($_FILES["img"]['name']))
{
$filename=$uid.$_FILES["img"]["name"];
}
$fileField=$filename;
Line Number 87 Is:
move_uploaded_file ($_FILES['img']['tmp_name'], $imgname);
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
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";
}