(The question was initially asked in Server Fault. I move it to here, because I don't have enough credit to start a bounty over there.)
I am trying to make my Windows Server 2012 R2 to host a webpage where users could upload a file.
<!-- indexfile.html -->
<form action="uploadfile.php" method="post" enctype="multipart/form-data">
Browse for File to Upload: <br>
<input type="file" name="file" id="file" size="80"> <br>
<input type="submit" id="u_button" name="u_button" value="Upload the file">
</form>
// uploadfile.php
<?php
$file_result = "";
if ($_FILES["file"]["error"] > 0)
{
$file_result .= "No File Uploaded or Invalid File ";
$file_result .= "Error Code: " . $_FILES["file"]["error"] . "<br>";
} else {
$file_result .=
"Upload: " . $_FILES["file"]["name"] . "<br>" .
"Type: " . $_FILES["file"]["type"] . "<br>" .
"Size: " . ($_FILES["file"]["size"] / 1024) . " Kb<br>" .
"Temp file: " . $_FILES["file"]["tmp_name"] . "<br>";
if (chmod("C:/inetpub/testaddbbacom/test", 0777))
$file_result .= "chmod sucessful!<br>";
else
$file_result .= "chomod NOT sucessful!<br>";
copy($_FILES["file"]["tmp_name"], "C:/inetpub/testaddbbacom/test/" . $_FILES["file"]["tmp_name"]);
$file_result .= "File Upload Successful!";
echo $file_result;
}
?>
The problem is, files can be uploaded into C:\Windows\Temp\, whereas copy does not work. As consequence, C:\inetpub\testaddbbacom\test is always empty. I did try to change the permission of this folder by chmod, but it does not really work. Here is a comparaison of the permissions of two folders:
I also tried move_uploaded_file, but it did not work either, probably due to the same permission reason...
Does anyone know how to solve this problem?
Replace,
copy($_FILES["file"]["tmp_name"], "C:/inetpub/testaddbbacom/test/" . $_FILES["file"]["tmp_name"]);
With the following,
move_uploaded_file($_FILES["file"]["tmp_name"], "C:/inetpub/testaddbbacom/test/" . $_FILES["file"]["name"]);
the difference is use file name i.e $_FILES["file"]["name"]) and not $_FILES["file"]["tmp_name"]) in destination path.
Related
I am new to PHP and am trying to create a page to upload a jpeg file. The webpage seems to run fine and it appears the file is uploading, however the file is not appearing on the server. Any help you can provide will be great.
The PHP code is:
<?php
$target_dir="/var/www/html/";
$fileName=$_FILES['file']['name'];
$target_file=$target_dir . basename($fileName);
$imageFileType=pathinfo($target_file,PATHINFO_EXTENSION);
$fileTempName=$_FILES["file"]["tmp_name"];
$fileType=$_FILES["file"]["type"];
$fileSize=$_FILES["file"]["size"];
$fileError=$_FILES["file"]["error"];
if(($fileType=="image/jpeg")&&($fileSize<100000)){
if($fileError>0){
echo "Return Code: " . $fileError . "<br />";
}
else{
echo "Upload: " .$fileName . "<br />";
echo "Type: " . $fileType . "<br />";
echo "Size: " . ($fileSize / 1024) . " kb<br />";
echo "Temp file: " . $fileTempName . "<br />";
if (file_exists($fileName)){
unlink($fileName);
}
move_uploaded_file($fileTempName,$target_file);
echo "<br><br>File Temp Name: " .$fileTempName."\r\n <br>";
echo "Uploaded file stored as : " .$target_file ."<br><br>";
}
}
else{
echo "File is not a JPEG or too big.";
}
?>
And the HTML code is as follows:
<html>
<body>
<form action="save2web.php" method="post" enctype="multipart/form-data">
<label for="file">Filename:</label>
<input type="file" name="file" id="file"/>
<br/>
<input type="submit" name="submit" value="Upload"/>
</form>
</body>
</html>
The problem is that, you don't check the returned value of move_uploaded_file() at all. But looks like, you're running your script on the host, and since most host disallow relative paths, you'd better provide an absolute.
So try, replacing:
$target_dir = "/var/www/html/"; // <- This is relative, which might be blocked due to security reasons
with
$target_dir = dirname(__FILE__) . "/var/www/html/"; // dirname(__FILE__) is a path to root
And then, make sure that the file was uploaded:
if (!move_uploaded_file(...)) {
// error
}
Presuming your server is linux from the path, you may need to change the permissions of the folder your trying to upload to, try changing the folder owner to www-data, this fixed the same issue for me.
Change your target directory like this. Add a .(dot) before the directory name.
$target_directory = './var/www/html';
I'm trying to upload and save file in dir called images through a form. Form looks like this:
<form name="spremi" action="spremaj.php" method="post" enctype="multipart/form-data">
<input type="file" id="file" name="image" />
<input type="submit" value="Send" name="send" />
</form>
And php script looks like this:
if (file_exists("images/" . $_FILES["file"]["name"]))
{
echo $_FILES["file"]["name"] . " already exists. ";
}
else
{
move_uploaded_file($_FILES["file"]["tmp_name"],"images/" . $_FILES["file"]["name"]);
echo "Stored in: " . "images/" . $_FILES["file"]["name"];
}
Script always prints message "already exists". I know that questions like this were already asked, but none of the answers helped me. This code didn't work either on localhost, or web server. Thank you.
In your html form you do not have a file parameter so your php needs to accept image so change
if (file_exists("images/" . $_FILES["image"]["name"]))
{
echo $_FILES["image"]["name"] . " already exists. ";
}else
move_uploaded_file($_FILES["file"]["tmp_name"],"images/" . $_FILES["file"]["name"]);
to
if (file_exists("images/" . $_FILES["image"]["name"]))
{
echo $_FILES["image"]["name"] . " already exists. ";
}else
move_uploaded_file($_FILES["image"]["tmp_name"],"images/" . $_FILES["image"]["name"]);
I'm trying to debug an issue with a WordPress 3.5.1 where I cannot upload media via HTTP at all; the media uploader simply says "HTTP error" and fails. To diagnose what's going on, I decided to write (i.e. copy from w3schools) a really basic PHP file uploader to see if there's something weird going on behind the scenes. But for some reason, the $_FILE structure doesn't contain any information at all, even in the most basic of examples:
file.php :
<html>
<body>
<form action="upload.php" method="post" enctype="multipart/form-data">
<label for="file">Filename:</label>
<input type="file" name="file" id="file"><br>
<input type="submit" name="submit" value="Submit">
</form>
</body>
</html>
upload.php :
<?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) . " kB<br>";
echo "Stored in: " . $_FILES["file"]["tmp_name"];
}
?>
In every browser I've tried, with every file I've tried, this just outputs:
Upload:
Type:
Size: 0 kB
Stored in:
and printing $_FILES shows that it is just an empty array.
I'm using PHP-5 on shared hosting (lunarpages), but the php.ini file has file_uploads on and the size of the files I tried is nowhere even close to the upload_max_filesize. I am ready to throw my laptop against a wall, so any help would save me a couple thousand dollars.
<html>
<body>
<form action="upload.php" method="post" enctype="multipart/form-data">
<label for="file">Filename:</label>
<input type="file" name="file" id="file"><br>
<input type="submit" name="submit" value="Submit">
</form>
<?php
phpinfo();
?>
</body>
</html>
This worked for me, $_FILES was not empty, using a common Debian installation, PHP 5.2; try to adjust the permissions of your files and direcotires to 777
I had a similar issue once with a shared host. Turned out that I had no tmp directory set, therefore nothing was being uploaded. Check with your host that your php.ini is set up correctly and that a tmp directory exists for uploads and that you have write permissions for it.
$target="Your Path";
if(move_uploaded_file($_FILES['file']['tmp_name'], $target))
{
echo "Upload: " . $_FILES["file"]["name"] . "<br>";
echo "Type: " . $_FILES["file"]["type"] . "<br>";
echo "Size: " . ($_FILES["file"]["size"] / 1024) . " kB<br>";
echo "Stored in: " . $_FILES["file"]["tmp_name"];
}
else
{
echo "Unable to move temp file to target.";
}
If it is not yet too late, you should check your apache logs. The most probable cause is that the apache - and so your php interpreter - has no permission to your predefined upload directory.
I am trying to use PHP to upload a CSV file (type:application/vnd.ms-excel) to my Unix box. And then FTP it to Mainframes.
The problem occurs in uploading when extra blank lines are added to the uploaded file at the end of each line, i can see this when i download the uploaded CSV file using ftp Command in cmd prompt.
No Extra lines appear in the CSV while viewing in Unix box.
When it FTPs it to mainframe i can see a dot added to each line. Hex Dec x'0D'
Could you please help me out in truncating this extra character while uploading?
My HTML File
<html>
<body>
<form action="upload_file.php" method="post" enctype="multipart/form-data">
<label for="file">Filename:</label>
<input type="file" name="file" id="file" />
<br />
<input type="submit" name="submit" value="Submit" />
</form>
</body>
</html>
My PHP Code "upload_file.php"
<?php
$allowedExts = array("csv", "CSV");
$extension = end(explode(".", $_FILES["file"]["name"]));
if (($_FILES["file"]["type"] == "application/vnd.ms-excel")
&& in_array($extension, $allowedExts))
{
if ($_FILES["file"]["error"] > 0)
{
echo "Return Code: " . $_FILES["file"]["error"] . "<br />";
}
else
{
echo "Upload: " . $_FILES["file"]["name"] . "<br />";
echo "Type: " . $_FILES["file"]["type"] . "<br />";
echo "Size: " . ($_FILES["file"]["size"] / 1024) . " Kb<br />";
if (file_exists("upload/" . $_FILES["file"]["name"]))
{
echo $_FILES["file"]["name"] . " already exists. ";
}
else
{
move_uploaded_file($_FILES["file"]["tmp_name"],
"upload/" . $_FILES["file"]["name"]);
$conn = ftp_connect("a.b.c.com") or die("Could not connect");
ftp_login($conn,"login","pass");
echo ftp_put($conn,"'dataset.name'","upload/" . $_FILES["file"]["name"],FTP_ASCII);
ftp_close($conn);
echo "Stored in: " . "upload/" . $_FILES["file"]["name"];
}
}
}
else
{
echo "Invalid file";
}
?>
You might want to take a look at the http://at2.php.net/manual/en/function.trim.php method.
You can open the file after storing it, iterating over each line and trim() them. You can print them back to the file (or just load everything, iterante and do a file_put_contents()). This should remove any whitespace from the beginning and the end before you upload it.
If you just want to remove it from the end, you can also use rtrim() or if you want to be more flexible you can do it with preg_replace() and something like /\s*$/.
I'm new to the concept of file uploading using a form in PHP. I would like to know a PHP script for the purpose of uploading a logo image onto a site. I tried couple of sample demos available on the web but I didn't get the image into my folder.
PS: I didn't understand many of the scripts given.
I would like to upload a logo image to replace my existing logo. How to do that?
I have a link 'Change Logo' on clicking which it should go to another page where I can use a file uploading form. {I would prefer if the form didn't have an action.} But where will the saved image be posted. I would like a customized script that would upload an image into my folder 'style/images/' as 'logo.png'.
Check out the following link, it will show a step by step procedure of file upload through the clear comments in the script itself. Hope this helps
http://www.reconn.us/content/view/30/51/
Step 1 - Copy paste the above script and save it in a folder
Step 2 - Create a folder with name images in the same folder where the .php file with the above script pasted.
Step 3 - Upload a file sizes not greater than 100kb. The output should be File Uploaded Successfully! Try again!
Step 4 - To check whether the file is uploaded, check the images folder, you will find the uploaded file in the folder.
OR Try this
<?
if ($_FILES["file"]["error"] > 0)
{
echo "Return Code: " . $_FILES["file"]["error"] . "<br />";
}
else
{
// echo "Upload: " . $_FILES["file"]["name"] . "<br />";
// echo "Type: " . $_FILES["file"]["type"] . "<br />";
// echo "Size: " . ($_FILES["file"]["size"] / 1024) . " Kb<br />";
// echo "Temp file: " . $_FILES["file"]["tmp_name"] . "<br />";
if (file_exists("images/" . $_FILES["file"]["name"]))
{
$_FILES["file"]["name"] . " already exists. ";
}
else
{
move_uploaded_file($_FILES["file"]["tmp_name"],
"images/" . $_FILES["file"]["name"]);
echo "Stored in: " . "images/" . $_FILES["file"]["name"];
}
}
?>
<html>
<body>
<form action="index.php" method="post" enctype="multipart/form-data">
<label for="file">Filename:</label>
<input type="file" name="file" id="file" class="validate[required] text-input" />
<input type="submit" name="submit" value="Attach file" >
</form>
<form id="form1" name="form1" action="">
<input type="hidden" id="filename" value="<?echo $_FILES["file"]["name"];?>"/>
</form>
</body>
</html>