PHP Upload CSV file Extra Carraige Return - php

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*$/.

Related

Cannot copy or move uploaded files in Windows Server

(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.

cURL Upload File to PHP

I'm currently trying to use the cURL executable to upload mp4 Files to a php script using the POST method. In the PHP file I'm checking the File format and all that kind of stuff. Here's the PHP file:
<?php
$allowedExts = array("jpg", "jpeg", "gif", "png", "mp3", "mp4", "wma");
$extension = pathinfo($_FILES['file']['name'], PATHINFO_EXTENSION);
if (($_FILES["file"]["type"] == "video/mp4") && ($_FILES["file"]["size"] < 2000000) && 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 />";
echo "Temp file: " . $_FILES["file"]["tmp_name"] . "<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"]);
echo "Stored in: " . "upload/" . $_FILES["file"]["name"];
}
}
} else {
echo "Invalid file";
}
?>
When I upload files using a normal HTML form it works properly. This is the HTML form i used:
<!DOCTYPE html>
<head>
<title></title>
</head>
<body>
<form action="upload_file.php" method="post" enctype="multipart/form-data" name="uploadedfile">
<label for="file"><span>Filename:</span></label>
<input type="file" name="file" id="file" />
<br />
<input type="submit" name="submit" value="Submit" />
</form>
</body>
</html>
But when I now try it using the cURL Client using this command:
"curl -F file=#test.mp4 http://localhost:1337/upload_file.php"
It displays me "Invalid File" in the console, which is normally shown when the file doesnt match the attributes im checking in PHP(for example not fitting the file type).
I hope you guys understand my problem and can help me! :)
Greets Steven
Your $_FILES["file"]["type"] check is failing because cURL doesn't make any guesses to the type and won't send that automatically in the POST request.
This is easily spoofed so it's not a great check anyway.
But to make your example work, try specifying the content type with the file parameter:
curl -F file=#test.mp4;type=video/mp4 http://localhost:1337/upload_file.php
Further to what drew010 wrote (I'd write a comment but don't have the reputation yet), trusting those values passed in may actually be a security risk and has been used to hack sites in the past so be careful.

In PHP, is it possible to upload files to the server?

In PHP, is it possible to upload files to the server? If so, then how can I create an upload form and insert the files to a specific page? Any extension images: .jpeg .gif or files: .txt .pdf etc. I am using XAMPP (localhost to view my website).
I have tried many tutorials, but there are lot of errors.
This is this HTML file:
<form action="fileupload.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>
And here is the PHP script:
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"];
}
The result is shown like this:
Upload: (file here)
Type: image/jpeg
Size: 757.521484375 Kb
Stored in: (temp file in C:)
But whenever I find my file in the tmp folder. It's nowhere to be found.
function getExtension($str)
{
$i = strrpos($str,".");
if (!$i)
{
return "";
}
$l = strlen($str) - $i;
$ext = substr($str,$i+1,$l);
return $ext;
}
$extension = getExtension($filename);
$extension = strtolower($extension);
if($extension=="jpg" || $extension=="jpeg" || $extension=="gif" || $extension=="txt" || $extension=="pdf" ){
echo "Upload: " . $_FILES["file"]["name"] . "<br />";
echo "Type: " . $_FILES["file"]["type"] . "<br />";
echo "Size: " . ($_FILES["file"]["size"] / 1024) . " Kb<br />";
move_uploaded_file($_FILES["file"]["tmp_name"],"upload/" .date("d_m_h_i_s"). $_FILES["file"]["name"]);
}
The getExtension function helps to get the extension of the file. The files will be uploaded to the upload folder.

how to display images saved in a folder after uploading it

i simply want that after i upload an image it will be redirected to a page that displays all the images. The images are saved in a folder. i have found some examples but they are so complicated, i just want to try the simple way first.
this is the HTML code for uploading image:
<form action="upload_image.php" method="post" enctype="multipart/form-data">
<label for="file">Filename:</label>
Title:<input type="text" name="title" id="title" />
<input type="file" name="file" id="file" />
<br />
<input type="submit" name="submit" value="Submit" />
this is the PHP code to save the other details in the database:
include('../IFM-mobile_website/include/connect.php');
$title=$_POST['title'];
if ((($_FILES["file"]["type"] == "image/gif")
|| ($_FILES["file"]["type"] == "image/jpeg")
|| ($_FILES["file"]["type"] == "image/pjpeg"))
)
{
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"]) . " Kb<br />";
echo "Temp file: " . $_FILES["file"]["tmp_name"] . "<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"]);
echo "Stored in: " . "../IFM-mobile_website/upload/" . $_FILES["file"]["name"];
}
}
}
else
{
echo "Invalid file";
}
any help would be much appreciated.
Your file uploaded path and the path in which you are showing image are different
uploading to : "upload/" . $_FILES["file"]["name"]
showing it from : "../IFM-mobile_website/upload/" . $_FILES["file"]["name"]
move_uploaded_file($_FILES["file"]["tmp_name"],
"upload/" . $_FILES["file"]["name"]);
echo "Stored in: " . "../IFM-mobile_website/upload/" . $_FILES["file"]["name"];
To show all the images in a folder :
$a = glob('Your/path/*.{jpg,gif,png}',GLOB_BRACE);
print_r($a);

Permission problem with php upload form

I am trying to create a simple php image uploading form. I appear to have some kind of permissions problem though and I'm not sure how to fix this...
Here's my code...
<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>
upload_file.php...
<?php
if ((($_FILES["file"]["type"] == "image/gif")
|| ($_FILES["file"]["type"] == "image/jpeg")
|| ($_FILES["file"]["type"] == "image/pjpeg"))
&& ($_FILES["file"]["size"] < 200000))
{
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("upload/" . $_FILES["file"]["name"]))
{
echo $_FILES["file"]["name"] . " already exists. ";
}
else
{
move_uploaded_file($_FILES["file"]["tmp_name"],
"upload/" . $_FILES["file"]["name"]);
echo "Stored in: " . "upload/" . $_FILES["file"]["name"];
}
}
}
else
{
echo "Invalid file";
}
?>
Here's the error I get...
Stored in: upload/81350042.jpgPHP Warning: move_uploaded_file(upload/81350042.jpg) [function.move-uploaded-file]: failed to open stream: Permission denied in D:\websites\tb123654\www\upload_file.php on line 27 PHP Warning: move_uploaded_file() [function.move-uploaded-file]: Unable to move 'C:\upload\php5BFB.tmp' to 'upload/81350042.jpg' in D:\websites\tb123654\www\upload_file.php on line 27
open your ftp client, right click on the folder you upload things to, attributes or propieties, set permission to 777.
or
chmod 777 yourfolder
There is no folder called "uploads" in "D:\websites\tb1234\www\" so the images can't be moved there. I think that's what your script is trying to do. Try to use absolute paths and see if that works.

Categories