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.
Related
So, I am working on a system where people working in a company can upload files to a system which will be sorted by departments. I have managed to get the files info(name, size, type), but the problem occurs while trying to upload the file.
I get:
move_uploaded_file(/Advanced Java Programming.pdf): failed to open stream: Permission denied
move_uploaded_file(): Unable to move 'C:\xampp\tmp\php1B99.tmp' to '/Advanced Java Programming.pdf'
I have set the permissions of the folder where the file needs to be uploaded to everybody (777).
Here's my code
<?php
$department = $_POST['department'];
$file = $_FILES['fileToUpload'];
echo "<b>Department: </b>" . $department . "<br>";
echo "<b>Name: </b>" . $file['name']. "<br>";
echo "<b>Size: </b>" . $file['size'] . " bytes<br>";
echo "<b>Type: </b>" . $file['type'];
move_uploaded_file($file['tmp_name'], "/". $file['name']);
?>
Try using an absolute path for your destination or at least start it with the DIR-constant, "/" ist not a valid (Windows)-path.
Also think about using the constant DIRECTORY_SEPARATOR as "/" is a *nix-standard, but as you're running on Windows, it should be "\" - using the constant will hold the right slash for every system.
In this php code I want to customize the image upload destination. with this php file, I have directory called uploads. I want to add all my uploaded images to this directory and store path in db. how can I do this?
<?php
// Assigning value about your server to variables for database connection
$hostname_connect= "localhost";
$database_connect= "image_upload";
$username_connect= "root";
$password_connect= "";
$connect_solning = mysql_connect($hostname_connect, $username_connect, $password_connect) or trigger_error(mysql_error(),E_USER_ERROR);
#mysql_select_db($database_connect) or die (mysql_error());
if($_POST) {
// $_FILES["file"]["error"] is HTTP File Upload variables $_FILES["file"] "file" is the name of input field you have in form tag.
if ($_FILES["file"]["error"] > 0) {
// if there is error in file uploading
echo "Return Code: " . $_FILES["file"]["error"] . "<br />";
} else {
// check if file already exit in "images" folder.
if (file_exists("images/" . $_FILES["file"]["name"])) {
echo $_FILES["file"]["name"] . " already exists. ";
} else {
//move_uploaded_file function will upload your image. if you want to resize image before uploading see this link http://b2atutorials.blogspot.com/2013/06/how-to-upload-and-resize-image-for.html
if(move_uploaded_file($_FILES["file"]["tmp_name"],"images/" . $_FILES["file"]["name"])) {
// If file has uploaded successfully, store its name in data base
$query_image = "insert into acc_images (image, status, acc_id) values ('".$_FILES['file']['name']."', 'display','')";
if(mysql_query($query_image)) {
echo "Stored in: " . "images/" . $_FILES["file"]["name"];
} else {
echo 'File name not stored in database';
}
}
}
}
}
?>
currently when I run the upload
I am getting warnings
Warning: move_uploaded_file(images/1409261668002.png): failed to open stream: No such file or directory in D:\xampp\htdocs\image-upload\index.php on line 29
Warning: move_uploaded_file(): Unable to move 'D:\xampp\tmp\php1C1F.tmp' to 'images/1409261668002.png' in D:\xampp\htdocs\image-upload\index.php on line 29
You must specify a correct path, the 'images/1409261668002.png' path doesn't exist if you dont create them and don't specify them.
if(move_uploaded_file($_FILES["file"]["tmp_name"],"images/" . $_FILES["file"]["name"]))) { .... }
You must specify the absolute path
You can use below code:
$image=basename($_FILES['file']['name']);
$image=str_replace(' ','|',$image);
$tmppath="images/".$image;
if(move_uploaded_file($_FILES['file']['tmp_name'],$tmppath))
{...}
Let me know if you have any query/concern regarding this.
I would like to check that a file uploaded to my OpenShift app has a text extension (.txt or .tab). Following some advice given here I wrote the following code, with echoes added to help debug:
$AllowedExts = array('txt','tab');
echo "AllowedExts: " . $AllowedExts[0] . " and " . $AllowedExts[1] . "<br>";
$ThisPath = $_FILES['uploadedfile']['tmp_name'];
echo "ThisPath: " . $ThisPath . "<br>";
$ThisExt = pathinfo($ThisPath, PATHINFO_EXTENSION);
echo "ThisExt: " . $ThisExt . "<br>";
if(!in_array($ThisExt,$AllowedExts) ) {
$error = 'Uploaded file must end in .txt or .tab';
}
echo "error echo: " . $error . "<br>";
On uploading any file, the echoed response was:
AllowedExts: txt and tab
ThisPath: /var/lib/openshift/************/php/tmp/phpSmk2Ew
ThisExt:
error echo: Uploaded file must end in .txt or .tab
Does this mean that OpenShift is renaming the file upon upload? How do I get the original filename and then check its suffix? More generally, is there a better way to check the file type?
$_FILES['uploadedfile']['tmp_name'] contains the name of a temporary file on the server (which can be moved with move_uploaded_file()). If you want to check the original name of the uploaded file on the client machine use $_FILES['uploadedfile']['name'].
That's not an Open Shift issue, it's the standard way of PHP.
For further details see http://php.net/manual/en/features.file-upload.post-method.php
For other ways to detect the file type see http://php.net/manual/en/ref.fileinfo.php
I've gone through some of the questions, but haven't found an answer to my question so here it goes.
I've written a stereotypical script for uploading small files. The script works, and the file is uploaded to the server. However, I can't get it to upload it to move to the right subfolder.
<?php
if ($_FILES["file"]["error"] > 0)
{
echo "Error: " . $_FILES["file"]["error"] . "<br>";
}
else
{
echo "Upload: " . $_FILES["file"]["name"] . "<br>";
echo "Type: " . $_FILES["file"]["type"] . "<br>";
echo "Size: " . ($_FILES["file"]["size"] / 1024) . " MB<br>";
}
if (file_exists("/enhstudios/clients/media/" . $_FILES["file"]["name"]))
{
echo $_FILES["file"]["name"] . " already exists. ";
}
else
{
move_uploaded_file($_FILES["file"]["tmp_name"],
"/enhstudios/clients/media/" . $_FILES["file"]["name"]);
}
?>
The error I get is:
Warning: move_uploaded_file(/enhstudios/clients/july.jpg) [function.move-uploaded-file]: failed to open stream: No such file or directory in /hermes/waloraweb013/b1311/moo.enhstudios/enhstudios/clients/fileuploadcode.php on line 20
Warning: move_uploaded_file() [function.move-uploaded-file]: Unable to move '/tmp/phpkfxGLm' to '/enhstudios/clients/july.jpg' in /hermes/waloraweb013/b1311/moo.enhstudios/enhstudios/clients/fileuploadcode.php on line 20
Permissions for this directory are set at 777.
Where have I gone wrong? I've tried all different combos of subdirectories and still can't get it to upload to the right one.
Are you sure you don't mix the /hermes/waloraweb013/b1311/moo.enhstudios/enhstudios/clients and the /enhstudios/clients/ directories?
I think you would like to move to the longer one, but the second arguments first / makes it an absoulte path.
You can add there the full path, or you can try to remove the first /. (This relativisation works only if the document root is /hermes/waloraweb013/b1311/moo.enhstudios/)
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'];