I have been trying to create an upload.php script however the condition doesn't seem to work and PHP can not move the uploaded file to the folder upload.
Apache2 log output below
PHP Warning: move_uploaded_file(): The second argument to copy() function cannot be a directory in /var/www/html/upload.php on line 18, referer: http://192.168.0.110/stream.php
PHP Warning: move_uploaded_file(): Unable to move '/tmp/phpC34Agu' to '/var/www/html/upload/' in /var/www/html/upload.php on line 18, referer: http://192.168.0.110/stream.php
Upload.php code
<?php
$target_path = "/var/www/html/upload/";
$target = $target_path . basename($_FILES['uploadedfile']['name'][0] );
if(move_uploaded_file($_FILES['uploadedfile']['tmp_name'] [0], $target_path))
{
echo "The file ". basename( $_FILES['uploadefile']['name'] [0]). " has been uploaded";
}
else {
echo "Sorry, there was a problem uploading your file.";
}
?>
I would also like php to execute this ffmpeg command directly afterwards, but i'm unsure where to insert it.
FFMPEG command
ffmpeg -re -i uploadedfile.name -vcodec copy -f mpegts udp://239.1.1.1:5000
Thanks for all your help.
Kind Regards,
Mark Couto
You need to specify the INDEX KEY of the file:
$_FILES['uploaded']['tmp_name'][0]
$target = $target . basename($_FILES["fileToUpload"]["name"][0]);
if(move_uploaded_file($_FILES['uploaded']['tmp_name'][0], $target))
{
echo "The file ". basename( $_FILES['uploadedfile']['name'][0]). " has been uploaded";
}
This is the part of code where the problem is
if(move_uploaded_file($_FILES['uploaded']['tmp_name'], $target))
$_FILES['uploaded'] should be $_FILES[ 'FileToUpload' ]
Try this
if(move_uploaded_file($_FILES['FileToUpload']['tmp_name'], $target))
<=
http://php.net/manual/en/features.file-upload.post-method.php
Related
My problem tried to create a small script that allows me to create a folder and within this a file (jpg) in the Firebase Storage. I found on the web that just placing the name of the folder before the name of the file should be generated, but the file is not generated and loaded. The file is only loaded when I manually create the folder in Storage.
My upload code:
public function upload(){
$this->bucket->upload(
file_get_contents($_FILES['uploadedfile']['tmp_name']),
[
'name' => "ena/" . $_FILES['uploadedfile']['name']
]
);
}
I am using the Kreait library. Thank you and I look forward to your support.
Thanks jeromegamez, the code that implements and works correctly for me. Whose logic is simple, it receives the file and moves it to the server and once moved it loads it to firebase storage.
$target_path = "uploads/";
$target_path = $target_path . basename( $_FILES['uploadedfile']['name']);
$name = $_FILES['uploadedfile']['name'];
if(move_uploaded_file($_FILES['uploadedfile']['tmp_name'], $target_path)) {
$myfile = fopen($target_path, "r") ;
echo "The file ". basename( $_FILES['uploadedfile']['name'])." has been uploaded";
$this->bucket->upload($myfile, [
'name' => $_POST["proyecto"] . "/" . $name
]);
} else{
echo "There was an error uploading the file, please try again!";
}
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 trying to do a php upload that will upload into a specific folder.
One would choose the file they wish to upload next to a dropdown box which is a folder list. This is because it organises files.
<?php
session_start();
if(!isset($_SESSION["USER"]["Admin"])){
header("Location: index.html?unath");
}
$folder = mysql_real_escape_string($_POST['loc']);
$target_path = "../../shared/docs/$folder";
$upload2 = $target_path . basename( $_FILES['uploadedfile']['name']);
if(move_uploaded_file($_FILES['uploadedfile']['tmp_name'], $upload2)) {
echo "The file ". basename( $_FILES['uploadedfile']['name']).
" has been uploaded";
} else{
echo "There was an error uploading the file, please try again!";
}
?>
Currently the code uploads the file into the "docs" folder and not docs/folder. Instead it puts the folder name in front of the file. For example- if the folder is called "abc" and my file is called robs.docx it will upload it to the main Docs folder and call it abcrobs.docx
You have a missing slash
Replace this line:
$upload2 = $target_path . basename( $_FILES['uploadedfile']['name']);
with:
$upload2 = $target_path ."/". basename( $_FILES['uploadedfile']['name']);
OR:
Replace this line:
$target_path = "../../shared/docs/$folder";
with:
$target_path = "../../shared/docs/".$folder."/";
You do not need mysql_real_escape_string because there's no SQL involved here.
If no database connection is established, mysql_real_escape_string returns null. So you're probably throwing away the $_POST['loc'] value.
You should never ever use user supplied values for manipulating anything on the filesystem without really, really thorough inspection of what you're going to manipulate. See Security threats with uploads.
Use var_dump liberally to see what your values look like at various stages and do some debugging.
You are missing a slash after $target_path
Add a / on the end of your $target_path:
$target_path = "../../shared/docs/$folder/";
You should properly escape your variables:
$target_path = "../../shared/docs/". $folder ."/";
I tried uploading a picture to a directory in my server with the code below. However, when i run it, I get this error:
Warning: move_uploaded_file(images/) [function.move-uploaded-file]: failed to open stream: Is a directory in /home/a2943534/public_html/add.php on line 24
Warning: move_uploaded_file() [function.move-uploaded-file]: Unable to move '/tmp/php7yEkDe' to 'images/' in /home/a2943534/public_html/add.php on line 24
What am I missing here, please?
<?php
include_once("connect.php");
?>
<?php
//This is the directory where images will be saved
$target = "images/";
$target = $target . basename( $_FILES['photo']['title']);
//This gets all the other information from the form
$title=$_POST['title'];
$name=$_POST['name'];
$describe=$_POST['describe'];
$pic=($_FILES['photo']['title']);
$url=$_POST['url'];
$country=$_POST['country'];
$endDate=$_POST['endDate'];
//Writes the information to the database
mysql_query("INSERT INTO `authors` VALUES ('$title', '$name', '$describe', '$pic', '$url', '$country', '$endDate')") ;
//Writes the photo to the server
if(move_uploaded_file($_FILES['photo']['tmp_name'], $target))
{
//Tells you if its all ok
$result = "The file ". basename( $_FILES['uploadedfile']['name']). " has been uploaded, and your information has been added to the directory";
}
else {
//Gives and error if its not
$result = "Sorry, there was a problem uploading your file.";
}
?>
<?php
// if the form has been submitted, display result
if (isset($result)) {
echo "<p><strong>$result</strong></p>";
}
?>
rather than writing
$target = $target . basename( $_FILES['photo']['title']);
you should write
$target = $target . basename( $_FILES['photo']['name']);
i think there is nothing like $_FILES['photo']['title']..
I think you make a mistake with
$target = $target . basename( $_FILES['photo']['title']);
Which should be
$target = $target . basename( $_FILES['photo']['name']);
This because title does not exists within the $_FILES['photo']
Also this error states it:
Unable to move '/tmp/php7yEkDe' to 'images/' in /home/a2943534/public_html/add.php on line 24
The to images/ does not contain your filename.
the error is obvious and self-explanatory.
the second parameter have to be a filename, not directory.
Apart from the SQL injection issue which seems to be present in every single PHP question that features MySQL here on SO, you should start debugging.
You're getting a pretty clear error on which line and what function call causes the error. Look the error up on Google, read the manual for the functions you're using.
Long story short: you should create two variables, print them before your function call, and figure out what's wrong.
<?php
$source = $_FILES['photo']['tmp_name'];
$target = "images/" . basename( $_FILES['photo']['title']);
echo "Moving '$source' to '$target'";
move_uploaded_file($source, $target);
You'll immediately see where the error occurs.
Hi am sorry but I have difficulties solving the problem. What i want to do is to upload file and then pass the directory but its nbot working the way I expect. The upload goes to a server to a file called 'uploads' but I cannot pass the directory , cuz its what I need, in later decode function:
Here's the code for uploading file am using:
<?php
// Where the file is going to be placed
$target_path = "uploads/";
/* Add the original filename to our target path.
Result is "uploads/filename.extension" */
$target_path = $target_path . basename( $_FILES['uploadedfile']['name']);
if(move_uploaded_file($_FILES['uploadedfile']['tmp_name'], $target_path))
{
echo "The file ". basename( $_FILES['uploadedfile']['name']).
" has been uploaded";
} else{
echo "There was an error uploading the file, please try again!";
}
?>
and then I want to pass directory to here just after $json = $api->decode ( ):it shld be between the brackets I put ($target_path . basename( $_FILES['uploadedfile']['name'])) but its not working and I dont know how to solve it. The case decode is supposed to get directory of the file which was just uploaded and then decode what is inside:
case "decode":
$json = $api->decode ($target_path . basename( $_FILES['uploadedfile'] ['name']));
$start='{"content":"';
$pos_start = strpos($json, $start);
$end='"}';
$pos_end = strpos($json, $end);
thanx for any advice, please help me out if you know how, thanx
The way you build your JSON looks flawed. Typically it's best to accumulate the data first, and do a json_encode as the very last step. This way your JSON is never accidentally mangled. It may still not be semantically valid but at least you can see why ;)
$data = array(
"content" => $target_path . basename( $_FILES['uploadedfile']['name'])
);
return json_encode($data);
I hope this helps:
$json = $api->decode ($target_path . basename( $_FILES['uploadedfile'] ['name']));
$o=json_decode($json);
echo $o->content;
if that doesn't work try:
$json = $api->decode ($target_path);
$o=json_decode($json);
echo $o->content;
Since you are using $target_path = $target_path . basename( $_FILES['uploadedfile']['name']); above.