uploading files to web directory with PHP - php

I am trying to make a simple uploading application from a web page:localhost/test.html. I am getting these errors:
Warning: move_uploaded_file(test/Blue hills.jpg): failed to open stream:
No such file or directory in C:\wamp\www\test.html on line 11
and
Warning: move_uploaded_file(): Unable to move 'C:\wamp\tmp\php376.tmp'
to 'test/Blue hills.jpg' in C:\wamp\www\test.html on line 11
Here is my code
<html>
<form enctype="multipart/form-data" action="test.html" method="POST">
Please choose a file: <input name="uploaded" type="file" /><br />
<input type="submit" value="Upload" />
</form>
<html>
<?php
$target = "test/";
$target = $target . basename( $_FILES['uploaded']['name']) ;
$ok=1;
if(move_uploaded_file($_FILES['uploaded']['tmp_name'], $target)) {
echo "The file ". basename( $_FILES['uploadedfile']['name']). " has been uploaded";
} else {
echo "Uploading Error.";
}

Probably the directory test doesn't exist. Add these lines to your code.
if (file_exists('test/')) echo 'Ok it wasn\'t that';
else echo 'Um, create a directory called test here: '.dirname(__FILE__);

Ensure that the a test/ directory exists in the directory where your script is located, then you can use
$out = dirname(__FILE__) . '/' . $_FILES['uploaded']['name'];
move_uploaded_file($_FILES['uploaded']['tmp_name'], $out);

Change directory permissions (CHMOD) to 777 via your FTP client (read,write,execute for owner,group,public).

Related

php simple upload files not working

I'm currently working on an extremely simplistic PHP file upload, but it fails to do anything at all. There is nothing in the specified directory.
PHP on the submit of my HTML form:
<?php
if ($_FILES["file"]["error"] > 0)
{
//error
echo "Something went wrong";
}
else
{
move_uploaded_file($_FILES["file"]["tmp_name"],
"/uploads/" . $_FILES["file"]["name"]);
}
?>
I suspect that something may be up with the fact that I'm using GoDaddy, since they've been quirky with php features in the past.
EDIT: Fixed underscore;
now I'm getting an actual error.
Warning: move_uploaded_file(/uploads/asd.docx) [function.move-uploaded-file]: failed to open stream: No such file or directory in /home/content/09/11461509/html/php/upload.php on line 11
Warning: move_uploaded_file() [function.move-uploaded-file]: Unable to move '/tmp/phpSIA5Jv' to '/uploads/asd.docx' in /home/content/09/11461509/html/php/upload.php on line 11
You're running your code from a different folder "php", from what I've gathered by your error messages. Try running the code from the root of your server, and then use uploads as your uploading folder.
Also make sure the folder has the proper write permissions set. Usually 755 and sometimes 777 although 755 is a safer setting.
As per OP's original posted code
Missing underscore between $ and FILES
move_uploaded_file($FILES["file"]["tmp_name"],
---^
<?php
if ($_FILES["file"]["error"] > 0)
{
//error
echo "Something went wrong";
}
else
{
move_uploaded_file($_FILES["file"]["tmp_name"],
"/uploads/" . $_FILES["file"]["name"]);
}
?>
Please add the enctype in form tag as multipart/form-data and PHP code should as follows-
First make sure that your files has been uploaded successfully in temp directory, then try to debug upload path and finally file moving functionality to specific directory.
<?php
if ($_FILES["file"]["error"] > 0) {
//error
echo "Something went wrong";
} else {
// try echoing the following codes first-
echo "Upload: " . $_FILES["file"]["name"] . "<br>";
echo "Type: " . $_FILES["file"]["type"] . "<br>";
echo "Stored in: " . $_FILES["file"]["tmp_name"];
// then try to debug upload path
// move_uploaded_file($_FILES["file"]["tmp_name"],"/uploads/" . $_FILES["file"]["name"]);
// take move_uploaded_file(); in a variable and then print_r($variable);
}
?>
I am referring you to read this post for upload problem the docx file using PHP.
Try this
<html>
<body>
<form action="" method="post"enctype="multipart/form-data">
<input type="file" name="file" id="file"><br>
<input type="submit" name="submit" value="Submit">
</form>
</body>
</html>
<?php
move_uploaded_file($_FILES["file"]["tmp_name"],
"upload/" . $_FILES["file"]["name"]);
echo "Stored in: " . "upload/" . $_FILES["file"]["name"];
?>
please write in form tag enctype="multipart/form-data"

PHP move_uploaded_file

I am trying to upload files to a server using PHP move_uploaded_file and I am getting the following error:
Warning: move_uploaded_file() [function.move-uploaded-file]: The second argument to copy() function cannot be a directory in /Users/Rick/Sites/upload/upload.php on line 7
Warning: move_uploaded_file() [function.move-uploaded-file]: Unable to move '/Applications/XAMPP/xamppfiles/temp/phpDlCZUd' to '/Users/Rick/Sites/upload/uploads/richardgregson' in /Users/Rick/Sites/upload/upload.php on line 7
Below is my code, nothing complicated.
if($_POST["upload"]){
$target_path = "/Users/Rick/Sites/upload/uploads/" . $_POST["name"];
if(move_uploaded_file($_FILES['uploadedfile']['tmp_name'], $target_path)){
echo "<div class='success'>The file " . "<span class='filename'>" . basename( $_FILES['uploadedfile']['name']) . "</span>" . " has been uploaded</div>";
} else {
echo "<div class='error'>There was an error uploading the file, please try again!</div>";
}
}
The permissions on the folder it is writing to are correct. I dont understand the error "cannot be a directory as the argument is where we are moving the file to so it has to be a directory.."
Thanks
Rick
The error is telling you what's wrong - $target_path (/Users/Rick/Sites/upload/uploads/richardgregson) is a directory
Try adding an extension.
$extension=end(explode('.', $_FILES["uploadedfile"]["name"]));
$target_path = "/Users/Rick/Sites/upload/uploads/" . $_POST["name"] . '.' . $extension;
Or perhaps your putting the file into a folder
$target_path = "/Users/Rick/Sites/upload/uploads/" . $_POST["name"] . '/' . $_FILES["uploadedfile"]["name"];
Note: you should not trust the user to upload only the files you want and you should sanitise $_POST["name"], all too easy to make that '../../' and post a PHP file.

php file not uploading to temp directory

//uploadForm.html
<html>
<body>
<form action="upload_file.php" method="post" enctype="multipart/form-data">
<label for="browseFile">Filename : </label>
<input type="file" name="file" id="browseFile"><br>
<input type="submit" name="submit" value="Submit">
</body>
</html>
//upload_file.php
<?php
$allowedExt = array("png","jpg");
$temp = explode(".",$_FILES["file"]["name"]);
$extension = end($temp);
echo "uploading...";
if((($_FILES["file"]["type"]=="image/png") || ($_FILES["file"]["type"]=="image/jpg")) && ($_FILES["file"]["size"] < 1000000))
{
echo "success";
if($_FILES["file"]["error"] > 0)
{
echo "error in uploading" . $_FILES["file"]["error"]."<br>";
}
else
{
echo "<p>uploaded successfully</p>";
}
}
else
echo "invalid file" ;
echo $_FILES["file"]["name"]."stored in ".$_FILES["file"]["tmp_name"]."<br>";
move_uploaded_file($_FILES["file"]["tmp_name"],"uploads/".$_FILES["file"]["name"]);
echo "moved Successfully";
?>
When I try to echo the temp directory name , it is blank . The uploaded files are missing .
I dont get it in the MAMP/htdocs folder neither in /tmp/ directory .
I dont have uploads directory in /MAMP/htdocs/ .Wont the program create a directory if it does not exist ?
In your final instructions, you have $_FILES['name']['tmp_name'] instead of $_FILES['file']['tmp_name'].
By the way, you have a few errors in your script:
Even if someone uploads an invalid file, you show them an error message, but you still move it to the final place.
$_FILES["file"]["type"] is a value sent by the browser (ie: the client). A malicious attacker may sent you any kind of file and disguise it as a image/png, and you are trusting it. You cannot trust this value. Instead, you could use getimagesize, which returns you an array that has the mime type of the image (and is detected by the server (ie: by you). To detect the mime-type of non-images, you can use FileInfo, concretely finfo_file.
Also, the php script will not create your uploads folder if it does not exist, and instead will show an error (and do nothing). You must create this folder first, and make sure that the user running your php script (usually the same that is running your http server) has write permissions on that directory.
edit: You don't see any uploaded file in your temp directory because (quoting http://www.php.net/manual/en/features.file-upload.post-method.php):
The file will be deleted from the temporary directory at the end of
the request if it has not been moved away or renamed.
$allowedExt = array("png","jpg");
echo $temp = explode(".",$_FILES["file"]["name"]);
$extension = end($temp);
echo "uploading...";
if ($_FILES["file"]["error"] > 0)
{
echo "Error: " . $_FILES["file"]["error"] . "<br>";
}
else
{
move_uploaded_file($_FILES["file"]["tmp_name"],
"upload/" . $_FILES["file"]["name"]);
echo "Stored in: " . "upload/" . $_FILES["file"]["name"];
}
$_FILES["name"]["tmp_name"] does not exist, it should be $_FILES["file"]["tmp_name"]

Image uploading on window server through php error

i got a error in image uploading on window based server where it works well on localhost as well as in linux server.Firstly i manually create a folder named as photos in root directory & two files named as imageupload.php & index.php
where code is
index.php
<html>
<body>
<form enctype="multipart/form-data" action="imageupload.php" method="post">
Choose Picture:<input type="file" name="photo"/>
<input name="submit" type="submit" value="save"/>
</form>
</body>
</html>
imageupload.php
<?php
if(isset($_REQUEST['submit'])){
$target = "photos/";
$finallink = $target.basename($_FILES['photo']['name']);
if(move_uploaded_file($_FILES['photo']['tmp_name'], $finallink))
{
echo "The file ". basename( $_FILES['photo']['name']). " has been uploaded, and your information has been added to the directory";
}
else {
echo "Sorry, there was a problem uploading your file.";
}
}
?>
this code works well on localhost & as well as on linux server but i purchased a window based hosting & where as I'm getting error on running this code on window based web server is .
Sorry, there was a problem uploading your file.
How to fix it ? Is this issue of php code running on window based server or anything else? Thanks for your help in advance.
Try this code hope it will hlp you
if(isset($_REQUEST['submit'])){ move_uploaded_file($_FILES["photo"]["tmp_name"],
"upload/" . $_FILES["photo"]["name"]);
echo "Stored in: " . "upload/" . $_FILES["photo"]["name"]; }
move_uploaded_file($_FILES["companylogo"]["tmp_name"],"companylogo/" . $_FILES["companylogo"]["name"]);
echo "Stored in: " . "companylogo/".$_FILES["companylogo"]["name"];

File upload using cakephp

I just want to upload a single pdf file using cakephp,
here is my view called pdfadd.ctp:
<?php echo $this->Form->create('pdfadd1', array('enctype' => 'multipart/form-data'));?>
<fieldset>
<?php
echo $this->Form->file('Document.submittedfile');
?>
</fieldset>
<?php echo $this->Form->end(__('Submit'));?>
Here is my conroller:
public function pdfadd(){
if ($this->request->is('post') || $this->request->is('put')) {
//die();
$file = $this->request->data['Document']['submittedfile'];
//$this->pdfadd1->save($this->request->data);
move_uploaded_file($this->data['Document']['submittedfile']['tmp_name'], $_SERVER['DOCUMENT_ROOT'] . '/app/webroot/files/' . $this->data['Document']['submittedfile']['name']);
}
It gives me this error:
Warning (2): move_uploaded_file(D:/Program Files D/xampp/htdocs/app/webroot/files/Functions Package for email (1).pdf): failed to open stream: No such file or directory [APP\Controller\PagesController.php, line 29]
Warning (2): move_uploaded_file() [function.move-uploaded-file]: Unable to move 'D:\Program Files D\xampp\tmp\php862.tmp' to 'D:/Program Files D/xampp/htdocs/app/webroot/files/Functions Package for email (1).pdf' [APP\Controller\PagesController.php, line 29]
And also I want to rename the file to 1.pdf. The file should save in webroot/files.
Replace this:
$_SERVER['DOCUMENT_ROOT'] . '/app/webroot/files/' . $this->data['Document']['submittedfile']['name']
with this:
WWW_ROOT . 'files' . DS . '1.pdf'
However, you really should do more validation, like using PHP's is_uploaded_file function, making sure the file really is a PDF, etc.
move_uploaded_file($this->data['Document']['submittedfile']['tmp_name'], $_SERVER['DOCUMENT_ROOT'] . '/app/webroot/files/' . $this->data['Document']['submittedfile']['name']);
1.Change this code according to directory
2.c:xampp/htdocs(its a default location for uploading in cakephp ),then put your upload file location
move_uploaded_file($this->data['Document']['submittedfile']['tmp_name'], $_SERVER['DOCUMENT_ROOT'] . 'cakephp/app/webroot/files/' . $this->data['Document']['submittedfile']['name']);
3.You can rename it before uploading
This line get only file name, not file.
$file = $this->request->data['Document']['submittedfile'];
You could use this.
$file = $_FILES ['Document'] ['submittedfile'];

Categories