I am trying to upload a file that has an Arabic name like (مرحبا بكم)
But when I upload it to server the name is not correct, it shows characters like that (ريÙ-Ù).
So, how can I upload files with Arabic name?
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>
PHP File:
<?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"];
}
?>
you have to encode the name in unicode.
you could use base64_encode($filename) to save the name of the file
Related
I use PHP to upload file to my server.I can't find my file in files folder.I suppose My code is right, but the file php.ini should be configured before uploading the file.Who can help me ? Here is my html code:
<form action="upload_file.php?act=put&name=use_resource" method="post" enctype="multipart/form-data">
<label for="file">PUT:</label>
<input type="file" name="file" id="file" />
<input type="submit" name="submit" value="Submit" />
</form>
My upload_file.php code is:
<?php
if ($_GET["act"] == "put")
{
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 />";
move_uploaded_file($_FILES["file"]["tmp_name"], "files/".$_FILES["file"]["name"]);
}
}
Your code is perfect and working, Did you give write access to 'files' folder?
I am using the following html form code to allow the user to select multiple files for upload.
<form action="uploadFiles.php" method="post" enctype="multipart/form-data">
<label for="file">Filename:</label>
<input type="file" name="file[]" multiple /><br>
<input type="submit" name="submit" value="submit" />
</form>
uploadFiles.php has the following code.
<?php
echo "uploadFiles.php" . "<br>";
print_r($_POST);
if(isset($_POST['submit']))
{
echo "Post submit" . "<br>";
if ($_FILES["file"]["error"][0] > 0)
{
echo "Error: " . $_FILES["file"]["error"][0] . "<br>";
}
else
{
echo "No. files uploaded : ".count($_FILES['file']['name'])."<br>";
echo "Upload: " . $_FILES["file"]["name"][0] . "<br>";
echo "Type: " . $_FILES["file"]["type"][0] . "<br>";
echo "Size: " . ($_FILES["file"]["size"][0] / 1024) . " kB<br>";
echo "Stored in: " . $_FILES["file"]["tmp_name"][0];
}
}
?>
For some reason, isset($_POST['submit']) always returns false. I get the following output
uploadFiles.php
Array( )
You aren't closing the input tags. Particularly, you aren't closing the <input type="file" name="file[]" multiple> tag, so possibly it isn't being added to the form's $_POST array correctly, and is instead being added to the file array. Just a guess.
The problem appears to be related to the size of the files. If the files are 11 MB in size, the post only works with one of them. If I select both $_POST is an empty array. If the files are half that size, the post works with both with the following code.
HTML
<form action="uploadFiles.php" method="post" enctype="multipart/form-data">
<label for="file">Filename:</label>
<input type="file" name="file[]" multiple="multiple"/></br>
<input type="submit" name="submit[]" value="submit" />
</form>
uploadFiles.php
<?php
if(isset($_POST['submit']))
{
if ($_FILES["file[]"]["error"] > 0)
{
echo "Error: " . $_FILES["file[]"]["error"] . "<br>";
}
else
{
$numFilesUploaded=count($_FILES['file']['name']);
echo "No. files uploaded : ".$numFilesUploaded."<br><br>";
for ($inc=0; $inc<$numFilesUploaded; ++$inc){
echo "File " . $inc . ": " . $_FILES["file"]["name"][$inc] . "<br>";
echo "Upload: " . $_FILES["file"]["name"][$inc] . "<br>";
echo "Type: " . $_FILES["file"]["type"][$inc] . "<br>";
echo "Size: " . ($_FILES["file"]["size"][$inc] / 1024) . " kB<br>";
echo "Stored in: " . $_FILES["file"]["tmp_name"][$inc];
echo "<br><br>";
}
}
}
?>
Below is my html code....
<form enctype="multipart/form-data" action="some.php" method="POST">
<label for="file">Filename:</label>
<input type="file" name="file" id="file"><br>
<input type="submit" name="submit" value="Submit">
</form>
and my some.php code...
print_R($_FILES);
print_r($_POST);
if ($_FILES["file"]["error"] > 0)
{
echo "Error: " . $_FILES["file"]["error"] . "<br>";
}
else
{
echo "Upload: " . $_POST["file"]["name"] . "<br>";
echo "Type: " . $_FILES["file"]["type"] . "<br>";
echo "Size: " . ($_FILES["file"]["size"] / 1024) . " kB<br>";
echo "Stored in: " . $_FILES["file"]["tmp_name"];
}
$_POST RESULTS IN Array ( [file] => gcc-mlion.tar [submit] => Submit )
BUT $_FILES gives empty result.
When you try to print File array that time your Spell of "print_r" is wrong.
You write "print_R" instead of "print_r", Php is case sensitive so it's matters a lot.
You're trying to output the value of $_POST['file']['name'];. It will return an undefined index error message.
Change that line to:
echo "Upload: " . $_FILES['file']['name'] . "<br>";
That should fix the issue.
Also, here's how I'd do it:
<pre>
<?php
if(isset($_POST['submit'])) //checking if form was submitted
{
print_r($_FILES);
print_r($_POST);
if ($_FILES["file"]["error"] > 0) //checking if error'ed
{
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"];
}
}
?>
</pre>
<form enctype="multipart/form-data" action="" method="POST">
<label for="file">Filename:</label>
<input type="file" name="file" id="file"><br>
<input type="submit" name="submit" value="Submit">
</form>
Hope this helps!
I have this form:
<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>
And this php script:
<?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"];
move_uploaded_file($_FILES["file"]["tmp_name"],
"/tmpupload/" . $_FILES["file"]["name"]);
echo "Stored in: " . "/tmpupload/" . $_FILES["file"]["name"];
}
?>
My goal is using curl from command line to upload a file.
The problem is to avoid the interaction with the button submit and choose, so the upload should me automatic.
Is there a solution?
To make your form auto submit on selecting the file change your file input to look like
<input type="file" onchange="formname.submit();" name="file" id="file" />
So make sure you give your form a "name" property and change 'formname' in the onchange to be the same as your chosen form name.
I am trying to upload a file using following code but the file is not getting saved in the desired location and no error is being popped out.
The php code is as follows`
<?php
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"];
}
}
?>
The html code is
<html>
<body>
<form action="index.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>
Check user permissions of the folder you want to store file to.