Hello I am trying to upload a file a user selects on their local computer to my server through a form but I get the following php error:
Warning: move_uploaded_file(bqformtest/uploaded_files/test.doc)
[function.move-uploaded-file]: failed to open stream: No such file or
directory in /home/drawapl1/public_html/bqformtest/index.php on line
40
Warning: move_uploaded_file() [function.move-uploaded-file]: Unable to
move '/tmp/phphhS4QD' to 'bqformtest/uploaded_files/test.doc' in
/home/drawapl1/public_html/bqformtest/index.php on line 40
This is my php code:
$target = "bqformtest/uploaded_files/";
$target = $target . basename( $_FILES['upload']['name']) ;
if(move_uploaded_file($_FILES['upload']['tmp_name'], $target))
{
echo "The file ". basename( $_FILES['uploadedfile']['name']). " has been uploaded";
}
else {
echo "Sorry, there was a problem uploading your file.";
}
This is my form code:
<form method='post' action='' accept-charset='UTF-8' enctype='multipart/form-data'>
<input type="file" name="upload" size="50" />
<input id="submitButton" type='submit' name='Submit' value='' />
</form>
The uploaded_files folders permissions is set to 755. Thanks in advance.
Provide full path to file, or right relative path
$target = "{fullPath}/fileName";
the path to file is wrong
From what I can tell based on the error message, your script is in ROOT/bqformtest. Then you are setting $target to relative path: bqformtest/uploaded_files. This means the script will try to move the uploaded file from the temp dir to: ROOT/bqformtest + bqformtest/uploaded_files resulting in:ROOT/bqformtest/bqformtest/uploaded_files. Set $target to:
$target = "uploaded_files/";
or
$target = "/home/drawapl1/public_html/bqformtest/uploaded_files/";
and it should work.
By the way, you are using the name of the uploaded file without sanitizing it which is a major security risk. Don't trust anything sent by the user.
Also Check disk quota for the user. I had this problem and and after many hours of testing I found it was simple. The user's quota was exceeded
Related
Sorry if this is easy but I'm slightly new to programming so I'm having problems renaming a temporary uploaded file. Here is my code:
HTML:
<input type="file" class="upload-img" name="logo" id="image" accept="image/*" />
PHP:
if(move_uploaded_file($_FILES['logo'] ['tmp_name'], "images/".[logo])) {
print '<p> Your image has been uploaded </p>';
} else {
print '<p> There was an error with the image</p>';
}
Would anyone be able to tell me how to correctly rename the temporary uploaded image to 'logo'?
I currently get an error that says:
Warning: fopen(images/logo.png): failed to open stream: No such file
or directory in /home/matos/public_html/pdftest/tfpdf.php on line 1443
FPDF error: Can't open image file: images/logo.png
first you should know the type of the file. $_FILES['logo']['type'] will give you the type. Check below code.
$extension = explode("/", $_FILES['logo']['type']);
$rename="logo.".$extension[1];
if(move_uploaded_file($_FILES['logo']['tmp_name'], "images/" . $rename)) {
print '<p> Your image has been uploaded </p>';
} else {
print '<p> There was an error with the image</p>';
}
What is the absolute path of images folder?
I assume that the form submit file to /pdftest/tfpdf.php
and /home/matos/public_html/pdftest/tfpdf.php is the script handle uploaded file. If the images folder's absolute path is /home/matos/public_html/images
your code should be
move_uploaded_file($_FILES['logo'] ['tmp_name'], "../images/logo.png")
Because the base path on your context is the folder of main script which I assume is /pdftest/tfpdf.php, it was /home/matos/public_html/pdftest/ on your local. When you save the uploaded file the real path will be
/home/matos/public_html/pdftest/../images/logo.png
shorted /home/matos/public_html/images/logo.png
I'm currently making a PHP application, where I want to insert data from a excel sheet in a MySQL database. I've done that so far, but the problem now is, that the excel file needs to be in the root directory. What I'm looking for is a button where I can browse the file, and from which i get the name and the directory. Any suggestions?
currently I just use $fileName="gefertigt.xlsx";
I tried it with JavaScript (from this tutorial), but since I'm a beginner it's to difficult for me.
The PHP filesystem has two functions, basename() and dirname(), which give the filename and directory, respectively. Thus your answer is:
function get_name_and_directory($file) {
return array(
'name' => basename($file),
'directory' => dirname($file)
);
}
So I found a good solution for my problem, which is quiet easier than I thought. Instead of getting the directory of the file, I uploaded the file to a folder called "uploads" in my root directory. Since i knew that the file(which will always be an .xlsx file) will always be the same, but always with new content, it replaces the current file in the folder uploads with the upladed file.
code in html:
<form action="dbconnection.php" method="post" enctype="multipart/form-data">
Datei auswaehlen
<input type="file" name="dateiHochladen" id="dateiHochladen">
<input type="submit" value="Hochladen" name="submit">
</form>
code in php:
$target_dir = "uploads/";
$target_file = $target_dir . basename($_FILES["dateiHochladen"]["name"]);
$uploadOk = 1;
$imageFileType = pathinfo($target_file,PATHINFO_EXTENSION);
if(move_uploaded_file($_FILES["dateiHochladen"]["tmp_name"], $target_file)) {
echo "The file ". basename( $_FILES["dateiHochladen"]["name"]). " wurde hochgeladen.";
} else {
echo "Fehler beim hochladen.";
}
I've tried heaps of different things to get my php file upload to work, even trimming it right back to the absolute basics. I've tried different scripts and this and that but it's still giving me errors.
So I have this php script:
if (isset($_POST['submit'])) {
$target_path = "uploads/";
$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!";
}
}
So as you can see, absolute bare minimum. This is my simple HTML form:
<form action="" enctype="multipart/form-data" name="uploadform" method="post" >
<input size="10" style="border:0px;" name="uploadedfile" type="file">
<input type="submit" name="submit" class="buttonUpload" value="Upload">
</form>
Any file I try and upload gives me the error. This isn't the first piece of code I've tried. I tried the code example of w3schools, that again gave me the file upload error.
I've checked my php.ini and allow file uploads is on.
It's on my local machine so no need to chmod the upload folder.
Any suggestions on why this may be happening ?
Cheers!
I am trying to make a simple file upload form using PHP. Here's my code:
<?php
$uploads_dir = '/uploads';
if(isset($_FILES['thefile'])){
$errors= array();
$file_name = $_FILES['thefile']['name'];
$file_size =$_FILES['thefile']['size'];
$file_tmp =$_FILES['thefile']['tmp_name'];
$file_type=$_FILES['thefile']['type'];
$tmp_name = $_FILES['thefile']["tmp_name"];
if($file_size > 2097152){
$errors[]='File size must be less than 2 MB';
}
if(empty($errors)==true){
move_uploaded_file($tmp_name, "$uploads_dir/$file_name");
echo "Success";
}
else{
print_r($errors);
}
}
?>
<!DOCTYPE html>
<html>
<head>
<title>Simple File Upload</title>
</head>
<body>
<form action="" method="POST" enctype="multipart/form-data">
<input type="file" name="thefile" />
<input type="submit"/>
</form>
</body>
</html>
I realize that I'm not limiting file types, but I'll worry about that once I can get a simple .jpg or .zip file uploaded. Using the above code, I go to the page on my local server located at
C:\wamp\www\simpleupload (this contains index.php, the file posted above)
When I select a small image file and click submit, I'm presented with the following errors:
Warning: move_uploaded_file(/uploads/session_timeout_formatting_bug.png): failed to open stream: No such file or directory in C:\wamp\www\project_fileshare\index.php on line 18
and
Warning: move_uploaded_file(): Unable to move 'C:\wamp\tmp\phpEFDE.tmp' to '/uploads/session_timeout_formatting_bug.png' in C:\wamp\www\project_fileshare\index.php on line 18
Line 18 is the line that calls the move_uploaded_file() function.
How do I fix this error? I have an 'uploads_dir' folder located in the same folder as my index.php file. (reference the file path above). What am I doing wrong here? I must be misunderstanding some small part of this process and have put my directory in the wrong place, or I'm doing something wrong in the code.
Can someone spot my mistake and tell me what I need to do to fix it?
You are working on windows and you've told PHP a location that is inaccessible (i.e. /uploads linux path).
Since you are working in windows and your document root is C:\wamp\www\simpleupload
Which means, your files are located like this:
C:\wamp\www\simpleupload\index.php (your upload script)
C:\wamp\www\simpleupload\uploads (where the files should be uploaded)
Why don't you use absolute path like this:
$uploads_dir = getcwd() . DIRECTORY_SEPARATOR . 'uploads';
The getcwd() function will return the current working directory for the executing PHP script (in your case index.php), so the $uploads_dir will now look like this: C:\wamp\www\simpleupload\uploads
Try this.
If your upload directory is in the same location as your index.php remove the "/" in your $uploads_dir variable. This, or add a "." before the slash because now it refers to the root which might be something else then your current working directory. Speaking of the devil; http://php.net/manual/en/function.getcwd.php
$uploads_dir = getcwd() . '\uploads';
$uploads_dir = __DIR__ . '\uploads'; #php > 5.6
Also, check if your directory is writeable for php:
http://php.net/manual/en/function.is-writable.php
Also what Latheesan said, better to go cross platform as I made the same mistake seen in my edit.
<?php
function buildPath(...$segments){
return join(DIRECTORY_SEPARATOR, $segments);
}
echo buildPath(__DIR__, 'uploads');
?>
And i would change
if(isset($_FILES['thefile'])){
for this
if($_FILE['thefile']['error'] === UPLOAD_ERR_OK){
Because I think that this is the best way to know if the user upload a file or the input is blank.
Or
if (isset($_FILE["thefile"]["name"])){
First question for quite a while. Essentially, I've got some code that (1) works perfectly in the live environment, (2) used to work in my home OSX environment but (3) doesn't work now.
The HTML:
<form id="upload_form" action="php/upload_class_list.php" method="post" accept-charset="UTF-8" enctype="multipart/form-data" target="upload_target" >
<label>File:</label>
<input name="myfile" type="file" size="35" />
<input id="upload_submit" type="submit" value="Upload" />
<iframe id="upload_target" name="upload_target" style="width:0;height:0;border:0px solid #fff;"></iframe>
</form>
The PHP file:
$destination_path = getcwd().DIRECTORY_SEPARATOR;
$result = 0;
$target_path = $destination_path . basename( $_FILES['myfile']['name']);
if(#move_uploaded_file($_FILES['myfile']['tmp_name'], $target_path)) {
...
}
sleep(1);
?>
<script language="javascript" type="text/javascript">top.upload_class_list(<?php echo $result; ?>);</script>
The script fires at the end, but the PHP code doesn't enter the if (in the local development environment) and so $result remains at 0.
It seems it's not picking up the path of the file to be uploaded; $destination_path points to the folder where the PHP file is located, and not where the file is found.
I think my local environment may have stopped working when I changed to Mountain Lion and rebuilt the PHP setup.
What is missing to stop the file being found?
Let me emphasise: exactly the same code works fine in my live Hostmonster setup, so it's an environment problem, I guess :)
Thanks.
if(#move_uploaded_file($_FILES['myfile']['tmp_name'], $target_path)) {
...
}
What is missing to stop the file being found?
What makes you think the problem is the file being found? Maybe the file is found, but what fails is the move... for example because the Web server has no permissions to write into the target_path.
You can check:
$src = $_FILES['myfile']['tmp_name'];
$dst = $target_path;
if (!file_exists($src))
die("Okay, the file is actually not found");
if (!is_readable($src))
die("Very bad hosting juju. The file was uploaded but I can't read it?!?");
if (!is_writeable($destination_path))
die("As expected, you can't upload a file here. This is a good thing.");
#touch($dst);
if (!file_exists($dst))
die("So call me a Marine, the file SHOULD be writeable (which is not so good), and yet I could not write it! Perhaps disk full? User overquota? Some weird security setup?");
The reason why it's a good thing is because that directory holds executable PHP files, and if anyone could upload a PHP file in there, well, that would be a major security hole.
You can set aside another directory and make it writeable (remember to put in there a .htaccess or other system to prohibit read/execute access from the outside).
may be this
<?php
$target = "upload/";
$target = $target . basename( $_FILES['uploaded']['name']) ;
$ok=1;
if(move_uploaded_file($_FILES['uploaded']['tmp_name'], $target))
{
echo "The file ". basename( $_FILES['uploaded']['name']). " has been uploaded";
}
else {
echo "Sorry, there was a problem uploading your file.";
}
?>