in upload form php beginner programming - php

I have made an upload form and PHP code to save file upload
HTML:
<HTML>
<head>
<title></title>
</head>
<body>
<form action='upload.php'method="post"enctype="multipart/formdata">
<input type="file" name="file" size="100000" />
<Br />
<input type="submit" value="Upload File" />
</form>
</body>
</HTML>
PHP:
<?php
if( $_FILES['file']['name'] != "" )
{
copy( $_FILES['file']['name'], __DIR__ ) or
die( "Could not copy file!");
}
else
{
die("No file specified!");
}
?>
<html>
<head>
<title>Uploading Complete</title>
</head>
<body>
</body>
</html>
I've changed my code to the form of tutorialspoint php pdf,but
still have a problem:
Warning: copy(111.jpg): failed to open stream: No such file or directory in C:\Program Files (x86)\EasyPHP-DevServer-14.1VC9\data\localweb\upload.php on line 4

The target path is incorrect. If you want to move dynamically the file to a "upload" directory inside your main directory, you can change :
if(move_uploaded_file($_FILES['file']['name'],".;C:\ProgramFiles\EasyPHPDevServer-14.1VC9\data\localweb"))
to
if (move_uploaded_file($_FILES["file"]["name"], __DIR__ . "/upload/" . basename($_FILES["file"]["name"]))) {

tnx all friend help my solve this.Ive done it by this script:
http://www.w3schools.com/php/php_file_upload.asp

Related

Trouble uploading a file using php (using a Linode server)

I am trying to upload an image though my php script (around 89kb in size). The code for my index.php is:
<html>
<head>
<title>File Uploading Form</title>
</head>
<body>
<h3>File Upload:</h3>
Select a file to upload: <br />
<form action="file_uploader.php" method="post"
enctype="multipart/form-data">
<input type="file" name="file" size="100" />
<br />
<input type="submit" value="Upload File" />
</form>
</body>
</html>
This is placed in the same directory as file_uploader.php
and then the code for file_uploader.php is:
<?php
if( $_FILES['file']['name'] != "" )
{
copy( $_FILES['file']['name'], "" ) or
//Exit and show the error message, die does this
die( "Could not copy file!");
}
else
{
die("No file specified!");
}
?>
<html>
<head>
<title>Uploading Complete</title>
</head>
<body>
<h2>Uploaded File Info:</h2>
<ul>
<li>Sent file: <?php echo $_FILES['file']['name']; ?>
<li>File size: <?php echo $_FILES['file']['size']; ?> bytes
<li>File type: <?php echo $_FILES['file']['type']; ?>
</ul>
</body>
</html>
The permissions of the directory index and file_uploader are in are : 755.
The script seems to load but I get the error "Could not copy file", not sure what is going on here. Could someone give me some pointers please? Thanks!!
Have a look at the documentation and then please explain what copy( $_FILES['file']['name'], "" ) is supposed to do? If I were you I would fix that statement as well as replace it with a more secure variant that was created specifically for this: move_uploaded_file.
Additionally, read through this section in the manual and you might find another bug in your code; if you'd use $_FILES[0]['tmp_name'] might make it work even better.
Putting all this together; I'd use:
move_uploaded_file($_FILES[0]['tmp_name'], __DIR__ .'/'. $_FILES[0]['name']);

File not being uploaded php

UPDATE
I have now solved it, the problem was that I did not have the preciding '/' and I did not have the '/' at the end, here is the final syntax of the folder path: $folder = '/home/dl/www/uploads/';
UPDATE
I have updated the code so that is can output some more detailed debugging information, and this is what is output:
Warning: move_uploaded_file(home/dl/www/uploads/test.txt): failed to open stream: No such file or directory in /home/dl/public_html/file-upload/upload.php on line 9
I have the below script which is attempting to upload a selected file to a specified directory on my web server. However, it is not uploading; I have checked that the permissions on the directory is at '777' as per numerous tutorials have suggested, but it is still throwing the 'File is not uploaded' message.
Does anyone have any suggestions as to why this may not be working?
Thanks!
<?php
$folder = "/home/dl/www/uploads";
if (is_uploaded_file($HTTP_POST_FILES['filename']['tmp_name'])) {
if (move_uploaded_file($HTTP_POST_FILES['filename']['tmp_name'], $folder.$HTTP_POST_FILES['filename']['name'])) {
Echo "File uploaded";
} else {
Echo "File not moved to destination folder. Check permissions";
};
} else {
Echo "File is not uploaded.";
};
?>
The HTML form:
<html>
<head>
<title>File Uploading Form</title>
</head>
<body>
<h3>File Upload:</h3>
Select a file to upload: <br />
<form action="upload.php" method="post"
enctype="multipart/form-data">
<input type="file" name="file" size="50" />
<br />
<input type="submit" value="Upload File" />
</form>
</body>
</html>
Try
$folder = "/home/dl/www/uploads/";
instead of
$folder = "/home/dl/www/uploads";
That is, a slash at the end.

unable to upload csv file in php

I was trying to upload a csv file in php. I also want it to be able to accept any type of file.
This is the code I'm using, however, it always enters the, "Could not copy file!" condition.
HTML code
<html>
<head>
<title>File Uploading Form</title>
</head>
<body>
<h3>File Upload:</h3>
Select a file to upload: <br />
<form action="upload.php" method="post" enctype="multipart/form-data">
<input type="file" name="file" size="50" />
<br />
<input type="submit" value="Upload File" />
</form>
</body>
</html>
PHP code
<?php
if( $_FILES['file']['name'] != "" )
{
copy( $_FILES['file']['name'], "/var/www/html/uploads" ) or
die( "Could not copy file!");
}
else
{
die("No file specified!");
}
?>
<html>
<head>
<title>Uploading Complete</title>
</head>
<body>
<h2>Uploaded File Info:</h2>
<ul>
<li>Sent file: <?php echo $_FILES['file']['name']; ?>
<li>File size: <?php echo $_FILES['file']['size']; ?> bytes
<li>File type: <?php echo $_FILES['file']['type']; ?>
</ul>
</body>
</html>
I have checked that the uploads folder and the html as well as php files are of permission 777, so they should be able to upload these files
EDIT
I have removed a lot of code from the php file and tried to make it upload(copy) a hardcoded file path from the filesystem.
What I observe is that, if I just run php upload.php from the command line, it works and "uploads" the file from the Downloads onto the Desktop. However,the same upload.php, if run from the browser and via index.html, on one of my servers, it just says, "Could not copy file!". On another server, it downloads the upload.php file. Basically, the same script works from the command line. Doesn't work from the browser.
(I don't see any error message etc on the browser console)
index.html
<html>
<head>
<title>File Uploading Form</title>
</head>
<body>
<h3>File Upload:</h3>
Select a file to upload: <br />
<form action="upload.php" method="post" enctype="multipart/form-data">
<input type="file" name="file" size="50" />
<br />
<input type="submit" value="Upload File" />
</form>
</body>
</html>
upload.php
<?php
$a = 2;
$b = 1;
if( $a > $b )
{
copy( "/home/PepperBoy/Downloads/myfile.csv", "/home/PepperBoy/Desktop/myfile.csv" ) or die( "Could not copy file!");
}
else
{
die("No file specified!");
}
?>
<html>
<head>
<title>Uploading Complete</title>
</head>
<body>
<h2>Uploaded File Info:</h2>
<ul>
<li>Sent file: <?php echo $_FILES['file']['name']; ?>
<li>File size: <?php echo $_FILES['file']['size']; ?> bytes
<li>File type: <?php echo $_FILES['file']['type']; ?>
</ul>
</body>
</html>
Replace this with your same line of code.
copy( $_FILES['file']['tmp_name'], "/var/www/html/uploads" ) or die( "Could not copy file!");
The name value in the $_FILES array doesn't contain the path to the file. Use tmp_name instead. You also need to give the copied file a name not just the destination directory. Also move the file rather than copy it or you will end up cluttering up the tmp directory
move_uploaded_file( $_FILES['file']['tmp_name'], "/var/www/html/uploads/". $_FILES['file']['name'] )
or die( "Could not copy file!");

warning message shows during the file upload using php

I am using ubuntu os with XAMPP. Here i've created two files uploader.php and phpEx6.php
when i try to upload a file it shows warning message.I am new to php.pls help to solve the problem..
phpEx6.php
<html>
<head>
<title>File Uploading Form</title>
</head>
<body>
<h3>File Upload:</h3>
Select a file to upload: <br />
<form action="uploader.php" method="post" enctype="multipart/form- data">
<input type="file" name="file" size="50" />
<br />
<input type="submit" value="Upload File" />
</form>
</body>
</html>
uploader.php
<?php
if( $_FILES['file']['name'] != "" )
{
error_reporting(E_ALL);
move_uploaded_file($_FILES['file']['tmp_name'], $_SERVER['DOCUMENT_ROOT'] . '/opt/lampp/htdocs/xampp/nf/uploads/' $_FILES['file']['name'])
or die("Couldn't copy the file.");
}
else
{
die("No file specified!");
}
?>
<html>
<head>
<title>Uploading Complete</title>
</head>
<body>enter code here
<h2>Uploaded File Info:</h2>
<ul>
<li>Sent file: <?php echo $_FILES['file']['name']; ?>
<li>File size: <?php echo $_FILES['file']['size']; ?> bytes
<li>File type: <?php echo $_FILES['file']['type']; ?>
</ul>
</body>
</html>
OUTPUT
Warning: move_uploaded_file(/opt/lampp/htdocs/opt/lampp/htdocs/xampp/nf/uploads/sam.txt): failed to open stream: No such file or directory in /opt/lampp/htdocs/xampp/nf/uploader.php on line 5
Warning:
move_uploaded_file(): Unable to move '/opt/lampp/temp/php1BAGC1' to '/opt/lampp/htdocs/opt/lampp/htdocs/xampp/nf/uploads/sam.txt' in /opt/lampp/htdocs/xampp/nf/uploader.php on line 5
try altering this
move_uploaded_file($_FILES['file']['tmp_name'], $_SERVER['DOCUMENT_ROOT'] . '/opt/lampp/htdocs/xampp/nf/uploads/'. $_FILES['file']['name'])
with this
move_uploaded_file($_FILES['file']['tmp_name'], $_SERVER['DOCUMENT_ROOT'] . '/xampp/nf/uploads/'. $_FILES['file']['name'])
or even this
move_uploaded_file($_FILES['file']['tmp_name'], '/opt/lampp/htdocs/xampp/nf/uploads/'. $_FILES['file']['name'])
The error is about, there's no folder created to which you are trying to move the files.. Create the folder and then try it or check whether the folder has all the permissions needed to upload the file into it..
Here's the sample code for Uploading a file..
if(isset($_POST['Submit']))
{
$tmp_name = $_FILES['Pic']['tmp_name'];
$name = $_FILES['Pic']['name'];
if(is_uploaded_file($tmp_name))
{
$org_name = time()."_".$name;
$dest = "uploads/$org_name";
move_uploaded_file($tmp_name, $dest);
}
}
there can be multiple answer for your questions as followed:
start path with DOT(.) such as './opt/foldername/'
create folder and give write permission to it in which you want to insert your file and data
or you can do before move uploaded in such as way just check first whether directory exist or not if its not than use mkdir function and than chmod to give write permission and than use upload function. its always a better practice to check directory existence and than only move the file there.
move_uploaded_file($_FILES['file']['tmp_name'], $_SERVER['DOCUMENT_ROOT'].'/xampp/nf/uploads/'. $_FILES['file']['name'])
#Peter Darmis

php script for uploading a file is not working as per expected

I have written a php code for uploading a file,but it seems there is
some issue with the code.can anyone plz help me out in resolving the
issue.
The HTML code is:
<html>
<head>
<title>File Uploading Form</title>
</head>
<body>
<h3>File Upload:</h3>
Select a file to upload: <br />
<form action="C:\xampp\htdocs\upload.php" method="post"
enctype="multipart/form-data">
<input type="file" name="file" size="50" />
<br />
<input type="submit" value="Upload File" />
</form>
</body>
</html>
And the PHP code is as follows:
<?php
if( $_FILES['file']['name'] != "" )
{
copy( $_FILES['file']['name'], "C:/xampp/htdocs" ) or
die( "Could not copy file!");
}
else
{
die("No file specified!");
}
?>
<html>
<head>
<title>Uploading Complete</title>
</head>
<body>
<h2>Uploaded File Info:</h2>
<ul>
<li>Sent file: <?php echo $_FILES['file']['name'] ; ?>
<li>File size: <?php echo $_FILES['file']['size']/1024; ?>
<li>File type: <?php echo $_FILES['file']['type']; ?>
<?php $_FILES['file']['tmp_name']; ?>
</ul>
</body>
</html>
I am using xampp to run the PHP scripts.
**Onclicking
upload file
button nothing is displayed as expected.**
So what is the output? Also, do print_r($_FILES) to show how it is prepared for you to access, you are likely to be accessing the filename inaccurately.
Use the proper function and the correct array element:
move_uploaded_file($_FILES['file']['tmp_name'], '...');

Categories