I write the php to upload file and scan the directory to show them as links, the scaning directoy works well I can see the text files I created in the directory, but I just can not move the local file to the desired directory .No file shows up after execution.
I think the problem may contain in this line:
move_uploaded_file($_FILES["file"]["tmp_name"],"/var/www/BlueTapeLogin/upload".$_FILES["file"]["name"]);
What I really want is to upload the image to the directory in /var/www/BlueTapeLogin/upload
and my php file lives in /var/www/BlueTapeLogin/upload_image.php
How can I change the code to make things work? Thanks in advance.
Please see my full code:
<html>
<head>
<?php
try
{
if (!empty($_POST["delete"])){
$delete=$_POST["delete"];
echo"we have the command delete this file:";
echo $delete;
$file = "upload/".$delete;
echo "/n***************";
echo "you want delete :";
echo $file;
echo "***************";
if (!unlink($file))
{
echo ("Error deleting $file");
}
else
{
echo ("Deleted $file");
}
}else{}
}catch(Exception $e)
{
echo 'Message: ' .$e->getMessage();
}
?>
<?php
move_uploaded_file($_FILES["file"]["tmp_name"],"/var/www/BlueTapeLogin/upload".$_FILES["file"]["name"]);
?>
<?php
$dir=scandir("/var/www/BlueTapeLogin/upload") ;
for($j=0;$j<count($dir);$j++){
echo $dir[$j];
echo"\n";
$target = $dir[$j]; // This is the file that already exists
$link = $dir[$j]; // This the filename that you want to link it to
echo "".$link."";
}
?>
</head>
<body>
<form action="upload_image.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"><br>
<label for="file">Delete</label>
<input type="text" name="delete" id="delete"><br>
<input type="submit" name="submit" value="Submit">
</form>
logout
</body>
</html>
You're missing a directory separator between upload and the filename, it should be:
move_uploaded_file($_FILES["file"]["tmp_name"],"/var/www/BlueTapeLogin/upload/".$_FILES["file"]["name"]);
The permissions you show say that only root can write into that directory, and but the webserver is probably using a userid like www-user. You need to change the ownership of the directory to the webserver userid. This will have to be done by the server administrator.
More likely, there's another directory that the webserver is already allowed to write into. The server administrator should be able to tell you what directory to use.
Check user permission to upload in that directory and try to make it simple. Just simply upload a file to other directory see it is working or not than check your code go one by one step.
Check return values check whether a warning is issued
Return Values
Returns TRUE on success.
If filename is not a valid upload file, then no action will occur, and move_uploaded_file() will return FALSE.
If filename is a valid upload file, but cannot be moved for some reason, no action will occur, and move_uploaded_file() will return FALSE. Additionally, a warning will be issued.
Related
I am new to HTML and PHP. I used html/php code from https://gist.github.com/taterbase/2688850 to implement a file upload page on my web server. Created "uploads/" folder in the server and gave chmod 777 permission to it.
It is working when I use name="uploaded_file", the original, I can see the file in the folder. However it is failing when I change the name="xxx" to something else, such as another name or the array form of it, no message on screen, file is not seen in the uploads folder.
Is the "uploaded_file" a fix or hard-coded kind of value? My aim is to use an array instead later, to make it for multiple files upload, however this name change restriction doesn't let me.
Please see below working and not-working samples:
upload.php (name="uploaded_file") Works
<!DOCTYPE html>
<html>
<head>
<title>Upload your files</title>
</head>
<body>
<form enctype="multipart/form-data" action="upload.php" method="POST">
<p>Upload your file</p>
<input type="file" name="uploaded_file"></input><br />
<input type="submit" value="Upload"></input>
</form>
</body>
</html>
<?PHP
if(!empty($_FILES['uploaded_file']))
{
$path = "uploads/";
$path = $path . basename( $_FILES['uploaded_file']['name']);
if(move_uploaded_file($_FILES['uploaded_file']['tmp_name'], $path)) {
echo "The file ". basename( $_FILES['uploaded_file']['name']).
" has been uploaded";
} else{
echo "There was an error uploading the file, please try again!";
}
}
?>
upload.php (name="user_file") Doesn't work
<!DOCTYPE html>
<html>
<head>
<title>Upload your files</title>
</head>
<body>
<form enctype="multipart/form-data" action="upload.php" method="POST">
<p>Upload your file</p>
<input type="file" name="user_file"></input><br />
<input type="submit" value="Upload"></input>
</form>
</body>
</html>
<?PHP
if(!empty($_FILES['user_file']))
{
$path = "uploads/";
$path = $path . basename( $_FILES['user_file']['name']);
if(move_user_file($_FILES['user_file']['tmp_name'], $path)) {
echo "The file ". basename( $_FILES['user_file']['name']).
" has been uploaded";
} else{
echo "There was an error uploading the file, please try again!";
}
}
?>
This line has the problem move_user_file($_FILES['user_file']['tmp_name'], $path).
php's bool move_uploaded_file ( string $filename , string $destination ) moves the uploaded files to a desired location. It is a built in function in php's API.
Your code should be changed to
move_uploaded_file($_FILES['user_file']['tmp_name'], $path)
in order to get it to work.
Your misunderstanding I think has come from your naming. Previously your field name is uploaded_file and the function name is move_uploaded_file(). So you must have thought that, when your field name is user_file, you should call move_user_file() in php. That's kinda creative thinking.. :D
Is the "uploaded_file" a fix or hard-coded kind of value? Well, you need to read the link given.
You are changing the core function call to either undefined function or a user defined function.
If you have defined a function move_user_file(), then its ok.
But, if you are expecting a core PHP inbuilt function, then there is no function move_user_file().
The line seems to have problem:
if(move_user_file($_FILES['user_file']['tmp_name'], $path)) {
Should be:
if(move_uploaded_file($_FILES['user_file']['tmp_name'], $path)) {
You are ending up in calling a undefined function move_user_file().
The required function is: move_uploaded_file()
Its not move_user_file, its move_uploaded_file. Next time, learn how to use your editor's find and replace correctly ;o)
I have searched all the questions of a similar nature yet have not been able to find the solution. I have checked my php.ini file and all permission and sizes are appropriate. The file is being created in mysql but I can not get a copy moved to my local folder. I have tried copy as well as setting permission with the commented out code seen below. Again, no errors are thrown that I can find.
Thank you. If you see the answer in another post feel free to supply a link.
<?php require_once("include/DB.php");?>
<?php require_once("include/Sessions.php");?>
<?php require_once("include/Functions.php");?>
<?php
// Check if image file is a actual image or fake image
if(isset($_POST["submit"])) {
$target_file = ($_FILES["Image"]["name"]);
$Target =__DIR__."\Uploads\\".basename($_FILES["Image"]["name"]);
$Query="INSERT INTO admin_panel (image)
VALUES('$target_file')";
$Execute=mysqli_query($Connection, $Query);
chmod($target_dir,0755);
if(move_uploaded_file($_FILES['Image'],['tmp_name'], $Target)){
$_SESSION["SuccessMessage"]="uploaded ". $Target;
Redirect_to("testImage2.php");
}else{
$_SESSION["ErrorMessage"]="not uploaded ". $Target;
Redirect_to("testImage2.php");
}
if($Execute){
$_SESSION["SuccessMessage"]="Post Added Successfully";
Redirect_to("testImage2.php");
}else{
$_SESSION["ErrorMessage"]="Something went wrong failed to add";
Redirect_to("testImage2.php");
}
}
?>
<!DOCTYPE html>
<html>
<body>
<?php echo Message();
echo SuccessMessage();
?>
<form action="testImage2.php" method="post" enctype="multipart/form-data">
Select image to upload:
<input type="file" name="Image" id="Image">
<input type="submit" value="Upload Image" name="submit">
</form>
</body>
</html>
pls i need help am trying to use the mkdir for user profile pic upload and its not working ... but when i use it normally i.e ordinarilly for example when i created a new php page and used " mkdir("newdir") "it worked
<?php
if (isset($_FILES['profilepic'])) {
if (((#$_FILES["profilepic"]["type"]=="image/jpeg") ||
(#$_FILES["profilepic"]["type"]=="image/png") || (#$_FILES["profilepic"]
["type"]=="image/gif"))&&(#$_FILES["profilepic"]["size"] < 1048576)) {
//1 Megabyte
$chars = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
$rand_dir_name = substr(str_shuffle($chars), 0, 15);
mkdir("userdata/profile_pics/$rand_dir_name");
}
else{
}
}
?>
<p>UPLOAD YOUR PROFILE PHOTO:</p> <br />
<form action="" method="POST" enctype="multipart/formdata">
<img src="./img/default_pic.png" width="70" />
<input type="file" name="profilepic" /><br /><br />
<input type="submit" name="uploadpic" value="Upload Photo"><br />
</form>
its not because of php, the php code is written well the problem is in the html where the html form enctype is written wrong
this
<form action="" method="POST" enctype="multipart/formdata">
should be
<form action="" method="POST" enctype="multipart/form-data">
Edit some code for your code with this:
Notice that it is best practise to use the variable outside of the string. Also you place the code in an if statement to check it executed correctly.
edit
You also need to start the mkdir call with a / or even better start it with $_SERVER['document_root'] so you know you always get an absolute path to the directory you want to generate.
if(mkdir("/userdata/profile_pics/".$rand_dir_name)){
print "this worked!";
}
else {
print "This failed";
}
Also you need to check that the permissions for the parent directory are correct, that the PHP usergroup is allowed to create directories in the /profile_pics/ folder.
You can also check that PHP is in the correct working directory, you can check this with print gwtcwd();
May be you should try like this (variable name along with a single-inverted comma)
mkdir("userdata/profile_pics/'$rand_dir_name'");
I hope it will work.
I am trying to upload selected image file into my ftp server on 1and1. I am not able to upload the file.
I have created folder called "uploadimages".
I have created a html form which has the following:
<form action="add.php" method="POST" enctype="multipart/form-data">
<div class="logo">
<label for="logoname" class="styled">Upload Logo (jpg / png):</label>
<div class="logofield">
<input type="file" id="logo" name="logo" size="30"/>
</div>
</div>
<input type="submit" value="Upload" name="submit" id="submit"/>
</form>
I have also create a php file which has the following:
<?php
$imagepic = $_FILES["logo"]["name"];
echo $imagepic;
$tempimgloc = $_FILES["logo"]["tmp_name"];
echo $tempimgloc;
$errorimg = $_FILES["logo"]["error"];
echo $errorimg;
if($errorimg > 0)
{
echo "<strong> <font size='18'>There was a problem uploading your Logo. Please try again!</font></strong>";
echo "<BR>";
}
else
{
move_uploaded_file($tempimgloc, "uploadimages/".$imagepic);
}
?>
ECHO printed results are:
1. Filename = testimage.png
2. Temp directory = /tmp/phpHvewUP
3. Error = 0
But they are not getting uploaded..
Where could I go wrong here?
Let me know!
You need to give access to your folder. Try this, chmod 777 uploadimages.
chmod 777 uploadimages
WRITING PERMISSION was the issue. Thanks Shapeshifter!
I would like to have the user upload a pdf to a folder on my website. (note:this is for learning purposes, so security is not necessary) The code I have below does not do echo a response when submitted. The folder I would like to have the pdf uploaded to is in the same directory as the php script, is it possible I'm incorrectly referencing that folder? I appreciate it.
<form method = "POST" action = "<?php echo $_SERVER['PHP_SELF']; ?>" enctype="multipart/form-data" method="post">
Email:<br /> <input type = "text" name="email" value=""/><br />
Resume:<br /><input type = "file" name="resume" value=""/><br />
<p><input type="submit" name ="submit" value="Submit Resume" /></p>
</form>
if(isset($_POST['submit']))
{
define ("FILEREPOSITORY","./resume/");
if (is_uploaded_file($_FILES['resume']['tmp_name'])) {
if ($_FILES['resume']['type'] != "application/pdf") {
echo "<p>Resume must be in PDF Format.</p>";
}
}else {
$name = $_POST['email'];
$result = move_uploaded_file($_FILES['resume']['tmp_name'], FILEREPOSITORY."/$name.pdf");
if ($result == 1) {
echo "<p>File successfully uploaded.</p>";
}
else {
echo "<p>There was a problem uploading the file.</p>";
}
}
}
You have a logical error. Your else statement should be part of the inner if statement -- not the outer one.
would suggest you check the permissions for the upload folder and the max size for file uploading in your php.ini... its happened to me many times uploading a file exceeding the limits and not getting an error message.. also the logic of your if else doesn't match as suggested by your previous post..
IT would be of great help to give the error you receive.
move_uploaded_file()
only works if you have the rights to write to the destination folder.