I'm trying to upload a photo to mysql database using php, however I'm getting the following error:
failed to open stream: Permission denied in '/The location of my php file' on line 40.
move_uploaded_file(): Unable to move '/Applications/XAMPP/xamppfiles/temp/phpgZi5gV to 'The location where the image will be stored'
Here is my code:
$dir = $path;
$default = "default.png";
// get Filename
$filename = basename($filen['name']);
$targetFilePath = $dir.$filename;
$filetype = pathinfo($targetFilePath, PATHINFO_EXTENSION);
if(!empty($filename)) {
//allowed file formats
$allowedFormats = array("jpg", "png", "jpeg", "gif", "pdf");
if(in_array($filetype, $allowedFormats)) {
if(move_uploaded_file($filen['tmp_name'], $targetFilePath)) {
return $targetFilePath;
}
}
Looks like you're dealing with some permissions issue where your application server doesn't have permission to move the images from their current folder /Applications/XAMPP/xamppfiles/temp/.
Can you change the permissions of the folder to see if that fixes the issue ?
Run the command bellow:
chmod 777 /Applications/XAMPP/xamppfiles/temp/
Related
I am making an image uploading feature for a program I am developing however I cant seem to get move_uploaded_file to actually move my file and I am not sure why.
I am receiving the form data as I can see the data when I use the print_r function print_r($_FILES)
My directory struct is as follows
root->app->lib->uploadCode.php and root->app->images
I dont get any errors.
I am running this on ubuntu 16.04 with php7 installed
Here is my code.
<?php
error_reporting(E_ALL);
if(!empty($_FILES['newImage']['name']))
{
$fname = $_FILES['newImage']['name'];
$fext = explode(".",$fname);
$sex = array("jpg", "jpeg", "JPG", "JPEG", "png", "PNG");//Supported extensions
//Check if file extension supported
$extRes = array_intersect($fext, $sex);//Checks to see if file uploaded extension exists in the supported extension array
if(count($extRes) == 1)
{
print_r($_FILES);
$name = $_FILES['newImage']['name'];
$ext = end($fext);
$tmp = $_FILES['newImage']['tmp_name'];
$path = '/../images/';
$newName = uniqid().".".$ext;
/*if(move_uploaded_file($tmp,$path.$newName))
{
echo "uploaded";
}
else
{
echo "not uploaded";
echo $path.$newName;
}*/
move_uploaded_file($tmp,$path.$newName);
}
elseif(count($extRes) > 1)
{//More than just one file extension was found possibly something like image.png.jpg
echo 02;
}
else
{// Nothing was found so wrong file type was used
echo 01;
}
}else
{//Do if no image was uploaded
echo 00;
}
`
Either remove the opening slash from $path = '/../images/'; or set the full path, e.g. /home/username/images/ for example
It was a permission issue.
I had to do the following to my www/html directory.
sudo chown -R username:www-data /var/www/html/
sudo chmod 775 /var/www/html/
sudo chmod g+s /var/www/html
Once I ran those commands I was able to upload
This question already has an answer here:
Permission denied while uploading a file
(1 answer)
Closed 7 years ago.
<?php
include_once 'database/dbconnect.php';
if(isset($_POST['btn-upload'])){
$file = rand(1000,100000)."-".$_FILES['file']['name'];
$file_loc = $_FILES['file']['tmp_name'];
$file_size = $_FILES['file']['size'];
$file_type = $_FILES['file']['type'];
$folder="uploads/";
move_uploaded_file($file_loc,$folder.$file);
$sql="INSERT INTO upload(file,type,size) VALUES('$file','$file_type','$file_size')";
mysql_query($sql);
}
?>
<?php
if( $_FILES['file']['name'] != "" ){
copy( $_FILES['file']['name'], "uploads/" ) or
die( "Could not copy file!");
}
else{
die("No file specified!");
}
?>
When I try to upload any file using xampp, I'm facing an error which says
Warning: copy(first.pdf): failed to open stream: No such file or directory in /opt/lampp/htdocs/new-project/upload.php on line 4
Could not copy file!
I even tried to change the folder permission by
sudo chmod -R 755 /opt/lampp/htdocs/new-project/
But, nothing as changed.
Configure The "php.ini" File
First, ensure that PHP is configured to allow file uploads.
In your "php.ini" file, search for the file_uploads directive, and set it to On:
file_uploads = On
Since name is the original name and not the path to the uploaded file you should use tmp_name instead:
// check so file is set and contains no error
if (isset($_FILES['file']) && $_FILES['file']['error'] == 0) {
$destination = 'uploads/' . $_FILES['file']['name'];
// use move_uploaded_file() instead of copy()
move_uploaded_file($_FILES['file']['tmp_name'], $destination);
}
else {
die("No file specified!");
}
If error is not equal to 0 you can check what kind of error occured following this link: File upload errors
i am getting an error in uploading image into folder. there is an error occured during uploading that image.
my Controller Code is (cakeplus is my root folder ex: htpp://localhost/cakeplus) :
$directory = "http://".$_SERVER['HTTP_HOST'].'/cakeplus/pics';
if(!is_dir($directory)) {
mkdir($directory,0777);
}
$files_array = $this->data['Photo']['path'];
//pr($files_array); die;
if(isset($files_array['name']) && $files_array['name']!='') {
$filetype = $files_array['type'];
$filesize = $files_array['size'];
$filename = $files_array['name'];
$filetmpname = $files_array['tmp_name'];
$file_type = explode('.',$filename);
$ext_name = array_reverse($file_type);
$final_file_title = $ext_name[1];
$file_name = Inflector::slug( str_replace( ".".$ext_name[0], "" , $filename ). '_' .time() ,'-' );
$newFileName = $file_name.'.'.$ext_name[0];
move_uploaded_file($filetmpname, $directory.'/'.$newFileName);
$requirementuploadData['Photo']['path'] = $file_name;
$this->Photo->create();
$this->Photo->save($requirementuploadData,false);
}
Error(s)(Warnings) :
Warning (2): move_uploaded_file(http://localhost/cakeplus/pics/wallpaper-1433586197.png): failed to open stream: HTTP wrapper does not support writeable connections [APP\Controller\PhotosController.php, line 31]
Warning (2): move_uploaded_file() [function.move-uploaded-file]: Unable to move 'C:\xampp\tmp\phpA80D.tmp' to 'http://localhost/cakeplus/pics/wallpaper-1433586197.png' [APP\Controller\Photos
Look into the CakePHP Upload plugin - it will abstract away much of the work that goes into dealing with file and image uploads.
The error you are seeing is because you cannot use move_uploaded_file() to transfer from a file path (C:\xampp\tmp\phpA80D.tmp) to an HTTP URL (http://localhost/cakeplus/pics/wallpaper-1433586197.png).
If you don't want to use the Upload plugin, and would prefer to keep working with what you already have, I would start by changing the $directory path. Something like this might be more appropriate:
$directory = WWW_ROOT . 'pics';
This will contain the path to your ./yourapp/webroot/pics directory, which is also the location of http://yourapp.com/pics.
Check out the documentation for more predefined paths.
may be folder dont have permission to write an image.
you should to use cakephp upload component.
$this->Upload->upload($file,$destination,$name);
I run my test php project on local computer using XAMPP. I have a form where user can browse image from their computer and upload to my database.
I use move_uploaded_file() function to move image to desired folder. The script runs without error but no image was moved to desired folder. It does not echo error message if move_uploaded_file() failed. Here is my code:
$upload_dir= 'uploads';
for($i=0; $i < count($_FILES['file']['tmp_name']);$i++){
if(!empty($_FILES['file']['name'][$i])){
$temp = explode(".", $_FILES["file"]["name"][$i]);
$extension = end($temp);
$allowedExts = array("gif", "jpeg", "jpg", "png");
if(!in_array($extension, $allowedExts)){
echo "Only image ending with .jpg, .jpeg, .gif, .png is allowed";
}
// copy the file to the specified dir
else{
$name[$i] = sha1(microtime()) . "." . $extension;
if(move_uploaded_file($_FILES['file']['tmp_name'][$i],$upload_dir.'/'.$name[$i])){
$image_path_array[$i]=$upload_dir.'/'.$name[$i];
}
else{
/*** an error message ***/
echo "error with move_uploaded_file";
}
}
}
}
Neither of your echo statements is likely to work that way. It should be
echo "error with move_uploaded_file";
No "=" sign.
Also, are you sure your web server has permission to write into the upload directory?
Try this:
1) I think you should give full directory path to your $upload_dir variable something like this:
$upload_dir=$_SERVER['DOCUMENT_ROOT']."/uploads";
2)Yes the echo that you have given for display "error with move_uploaded_file" need to correct other wise it will give syntax error:
//an error message
echo "error with move_uploaded_file";
I hope this fix your problem :)
You should have separate move_uploaded_files() function for image on server and image on client machine.
Image from client machine will be uploaded to your server temp folder
Image on online server will be fetched as url to that image
after uploading prestashop from my localhost to the server , a problem showed up
when trying to upload product image i got this error "Server file size is different from local file size"
after searching in prestashop files i found that is uploader class is responsible for the upload process.
public function upload($file, $dest = null)
{
if ($this->validate($file))
{
if (isset($dest) && is_dir($dest))
$file_path = $dest;
else
$file_path = $this->getFilePath(isset($dest) ? $dest : $file['name']);
if ($file['tmp_name'] && is_uploaded_file($file['tmp_name'] ))
move_uploaded_file($file['tmp_name'] , $file_path);
else
// Non-multipart uploads (PUT method support)
file_put_contents($file_path, fopen('php://input', 'r'));
$file_size = $this->_getFileSize($file_path, true);
if ($file_size === $file['size'])
{
$file['save_path'] = $file_path;
}
else
{
$file['size'] = $file_size;
unlink($file_path);
$file['error'] = Tools::displayError('Server file size is different from local file size');
}
}
return $file;
}
when commenting if statement witch responsible of comparing file size and tring to upload an image i got this error
"An error occurred while copying image, the file does not exist anymore."
i changed img folder permissions to 777 still same problem ?
Its a permission issue. I resolved it by giving complete access (777) to cache and theme folders.
If that doesn't help then try giving complete access to all the files. Be sure to change back the permission to 755 for folders and 644 for files for security reasons.
please change " if ($file_size === $file['size']) " to " if ($file_size = $file['size']) "
This method worked for me.