Use of move_uploaded_file() function issue - php

I have hosted my website to my server.The website is functioning well as same as the localhost.However,When i try to move uploaded files to the uploads folder,the move_uploaded_file() function is not working as it is working with the localhost.On using the mysql_error() function to find the error,it is not giving me any results.What is the problem with my code
code one is not giving me any results
code 1
if(move_uploaded_file($_FILES['Upfile']['tmp_name'],$location.$_FILES['Upfile']['name'])){
echo'proceed';
}else{
echo mysql_error();
}
code two is outputing the else phrase
echo'cannot move file to folder';
code 2
if(move_uploaded_file($_FILES['Upfile']['tmp_name'],$location.$_FILES['Upfile']['name'])){
echo'proceed';
}else{
echo 'cannot move file to folder ';
}

Make sure that the second parameter i.e destination where you are storing the uploaded file should be in string format.

Related

PHP rename() function not working properly. Alternative access is denied (code: 5)

I am facing a strange situation with php rename() function. I download a file using API and then moved that file to destination folder.
$download_path =
C:\Users\JOHN\AppData\Local\Temp/downloaded_folder/downloaded_file.png
$moving_path = C:\xampp\htdocs\project-1/images/downloaded_file.png
Every time downloading is working perfectly and file downloaded
The rename() function is working alternatively. ie on first time it works second time fails. 3rd time ITS works 4th time fails etc.
ie first time file moved . and second time i am getting warning
Warning:
rename(C:\Users\JOHN\AppData\Local\Temp/downloaded_folder/downloaded_file.png,C:\xampp\htdocs\project-1/images/downloaded_file.png):
Access is denied (code: 5)
Warning: copy(C:\xampp\htdocs\project-1/images/downloaded_file.png):
Failed to open stream: Permission denied
Please see my code below.
if (file_exists($moving_path)) {
#unlink($moving_path);
}
if (file_exists($download_path) && !file_exists($moving_path)) {
if (!rename($download_path, $moving_path)) {
if (copy ($download_path, $moving_path)) {
echo 'no rename but copied';
} else {
echo 'not moved';
}
} else {
echo 'moved';
}
#rmdir(dirname($download_path))
}
Here what happen is first time it's moved and downloaded_file.png coming inside project-1/images folder & echo moved and second time there is no file coming and echo not moved .
Third time downloaded_file.png coming inside project-1/images folder & echo moved and 4th time there is no file coming and echo not moved.
How to solve this issue. Please help. I seen a similar question but that answer also not working for me
It seems that you do not have the correct folder permissions for:
C:\xampp\htdocs\project-1/images/
As you have this directory showing up in your error for the rename and the copy functions. Set this directory's permissions to 750, for your basic needs.
Since you're using windows:
Right Click>Properties>Security Tab
Change the permissions for your user the script is running under to "Read & execute" and "Write".

Php Problem with move_uploaded_file. It seems everything is fine but it is not working

I am trying to use move_uploaded_file but I think everything is fine but my code is not working. Everything is working as expected even image name is being inserted into DB but it is not moving into uploads folder. Folder is there and apache2 does have access to write it.
I don't have enough reputation to post more then 8 lines so everything is on pastebin Here. The main aprt is...
$post_image_new_name = uniqid('', true). "."
.$post_image_actual_ext;
$post_image_destination = '../../uploads/';
if (is_dir($post_image_destination) && is_writable($post_image_destination)) {
$post_image_destination = '../../uploads/'.$post_image_new_name;
var_dump($post_image_destination);
move_uploaded_file($post_image_new_name, $post_image_destination);
echo "Inside move_uploaded_file section";
https://pastebin.com/Z321R76z
Try this code
line 53: move_uploaded_file($_FILES['addPost_post_image']['tmp_name'], $post_image_destination);
The first param should be the file source name.
move_uploaded_file() expects the source filename of the uploaded file. So you must use something like this:
move_uploaded_file($_FILES['addPost_post_image']['tmp_name'], $post_image_destination);

How to insert data into database in a multiple file input?

I have a code that lets a person upload multiple images through a form.
Problem is, the images upload fine on to the server but not sure how to get the images to be sent into the database.
PHP:
else{ // No error found! Move uploaded files
if(move_uploaded_file($_FILES["files"]["tmp_name"][$f], $targetscreenshots.$name))
$count++; // Number of successfully uploaded file
}
Where do I put the following code?
{
mysql_query("INSERT into Colleges (`files`) VALUES ('$files')"); // inserting data if file is moved
echo "Your screenshots have been uploaded successfully!"
}
This is my own code which i am using in my script.
<?php
$upath="../images/";
//uploads is the name of file array that is being uploaded.
foreach ($_FILES['uploads']['name'] as $key=>$file) {
$target = $upath.$file;
$path=substr($target,3);
// echo $path; THIS CAN BE STORED DIRECTLY TO THE DATABASE
move_uploaded_file($_FILES['uploads']['tmp_name'][$key], $target)
or die();
mysql_query(**YOUR INSERT QUERY HERE. IT WONT BE EXECUTED IF IMAGE IS NOT UPLOADED PROPERLY.**)or die(mysql_error());
}
?>
I read your comment and so i gave this answer... Kindly correct me if i have misinterpreted your question.
You are missing code that is responsible for database modification, I suggest you read some tutorials like this one.
I haven't tested it, but at least it looks like it involves all the steps required.
$files = $_FILES["files"]["tmp_name"][$f]
Just Insert the file path or name in the DB

File not readable in Laravel

Im trying to open a template with PHPExcel, in laravel.
$objPHPExcel = PHPExcel_IOFactory::load(URL::to_asset('myfile.xls'));
However, I get a File is not readable error.
When I do this...
if(is_readable(URL::to_asset('myfile.xls')){
echo "is readable";
} else { echo "nope";}
I get "nope" returned to me.
If i echo an image from this same asset directory, it displays fine.
<img src='URL::to_asset('img/test.png')'>
However if i do this....it also return "nope"
if(is_readable(URL::to_asset(img/test.png')){
echo "is readable";
} else { echo "nope";}
What is going on here?
Why if I visit the link in my browser the files display fine.
yet when I test them for readbility, they return false results??
This is Larave 3
Thanks
The PHP function is_readable() expects a system file as argument, so does PHPExcel_IOFactory::load(); you are passing a URL instead, which will always fail.
You need the local system path to the file and use that instead.

File upload php with file array

i try to upload files into my server also to my database.
However, i cannot upload them.
I can see my two files with below code,
echo $_FILES['file']['name'][0];
echo $_FILES['file']['name'][1];
$fileName = $_FILES['file']['name'][0];
$add = "img01.imgsinemalar.com/images/haber_anasayfa/" . $fileName;
if (move_uploaded_file($_FILES[file_up][tmp_name], $add)) {
NCore::db('NEWS')->insertAsArray(array('CONTENT' => $_POST['TvSpotdesc'], 'ADD_DATE' => $date, 'TITLE' => $_POST['newsTitle'],IMAGE_MAIN => $add))->execute();
echo "Haber başarıyla gönderildi.";
} else {
echo "Failed to upload file Contact Site admin to fix the problem";
}else {
echo $msg;
}
But above code does not upload my files, it goes directly to the else statement, why?
Failed to upload file Contact Site admin to fix the problem
Your code should be outputting two notices and a warning from this line:
if (move_uploaded_file($_FILES[file_up][tmp_name], $add)) {
The two notices would be because you have not quoted file_up and tmp_name and the warning to inform you of the reason move_uploaded_file has failed, as documented. You should check out the logs to see these for yourself (development without seeing any error messages is not fun).
The reason it fails is that you cannot move a file to a remote server -- $add is not a local path.
Rename $_FILES[file_up][tmp_name] to $_FILES['file']['tmp_name']
There is no such variable as file_up as per the code you pasted. Also, files can only be stored on the same server. so gove relative path like /home/www/myapp/images/ instead of server name.

Categories