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.
Related
First time i try to create a simple form using the POST method.Problem is when i click the button nothing gets echoed.
here is my insert.php file :
<?php
if(isset($_POSΤ["newitem"])){
echo $itemnew = $_POSΤ["newitem"];
}
?>
<form action="insert.php" method="POST" >
<input type="text" name="newitem">
<input type="submit" value="Save">
</form>
EDIT: I tried the GET method and it works...Any ideas why that happened? Server configurations?
NEW EDIT: So it turns out i switched method to GET and it worked.Then i switched back to POST (like the code i posted on top) and it works...I have no clue why this happened.Any suggests?
The code you have posted is perfectly valid and should work.
I'm going to guess that you do not have PHP enabled, or it is not working.
<?php ... ?> looks to the browser like a long, malformed HTML tag, and therefore ignores it, making the effect invisible.
Try right-clicking the page and selecting View Source. If you see your PHP there, then the server is indeed not processing it.
The most likely reason for this is probably the same problem I had with my very first bit of PHP code: you're trying to "run" it directly in your browser. This won't work. You need to upload it to a server (or install a server on your computer and call it from there)
Use !empty($_POST['newitem'] instead:
if(!empty($_POSΤ["newitem"])){
echo $itemnew = $_POSΤ["newitem"];
}
empty()
Try the following:
if($_POST) {
if(!empty($_POST['newitem'])) {
$itemnew = $_POSΤ['newitem'];
echo $itemnew;
// or leave it as is: echo $itemnew = $_POSΤ['newitem'];
}
}
?>
<form action="insert.php" method="POST" >
<input type="text" name="newitem">
<input type="submit" value="Save">
</form>
The if($_POST) will make sure the code is only executed on a post. The empty() function will also check if it isset() but also checks if it is empty or not.
Try this :
<?php
if(isset($_POSΤ["newitem"])){
echo $itemnew = $_POSΤ["newitem"];
}
?>
<form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="POST" >
<input type="text" name="newitem">
<input type="submit" value="Save">
</form>
$_SERVER['PHP_SELF']; is pre-defined variable in php.It allows the user to stay on same page after submitting the form.
I was wondering that each time i refresh my page do I have to somehow clear $_FILES , or why is it echoing back to me that the variable is set and also NOT empty? when i first load, or reload the page is obviously is at least empty by using print_r
<html>
<form action="" 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>
</html>
<?php
if(isset($_FILES["file"]) && !empty($_FILES["file"])){
echo 'You have just uploaded '. $_FILES["file"]["name"];
}
?>
Actually, You are facing this trouble because, after once you submit by selecting a file the browser caches the page response & when you refresh it by F5 or right click->reload it simply returns the cached result. You can try your old code by reloading it by a dummy GET request it will work fine. For example :
If the url to this page is :http://localhost/file.php then type in the address bar as : http://localhost/file.php/?dummy=abc then you will find your code works correctly... So, you must verify not just $_FILES but $_FILES["file"]["name"] instead, would go fine...
Try this, you wont have any trouble....
<html>
<form action="" 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>
</html>
<?php
if(isset($_FILES["file"]) && !empty($_FILES["file"]["name"])){
echo 'You have just uploaded '. $_FILES["file"]["name"];
}
?>
check the form has been posted,else your code while page loading
<?php
if($_POST['submit']=="submit"){
if(isset($_FILES["file"]) && !empty($_FILES["file"])){
echo 'You have just uploaded '. $_FILES["file"]["name"];
}
}
?>
action="" means you post the data to the current page, but after you post the data, when you refresh your page, the browser will ask you whether to repost the data, and if you press yes, then the data will be posted again.
The right solution is redirect to the current page after post.
The Answer by #Karthick Kumar is wrong because when there is an <input type="file" name="file">, even if no file selected by user, the $_FILES["file"] would never be empty (use print_r() and see it for yourself)!
#Anirban's answer is completely true, I'm just gonna explain it in a more convincing way:
echo( !empty($_FILES["file"]["name"]) ); //works as expected
echo( !empty($_FILES["file"]["type"]) ); //works as expected
echo( !empty($_FILES["file"]["tmp_name"]) ); //works as expected
echo( $_FILES["file"]["size"] > 0 ); //more convincing way!
And the best way would be:
echo( $_FILES["file"]["error"] == 0 ); //you can check the cause of error this way!
Link to the list of error codes
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.
I'm working with php/html5 and i'm attempting to upload a file, but $_FILES['picture'] never seems to contain anything. I've been through a lot of posts and looked for common fixes, but none of them seem to work, firstly, the code;
Form;
<form enctype="multipart/form-data" action="decodeQR.php" method="post">
<input type="hidden" name="MAX_FILE_SIZE" value="30000000" />
<input type="file" name="picture" id="picture" value="picture" accept="picture/*" capture>
<input type="submit" value="Upload">
</form>
decodeQR.php;
<?php
include 'header.php';
$upload_status = FALSE;
if(isset($_FILES['picture']))
{
echo 'picture set <br>';
}
else
{
echo 'picture not set <br>';
}
if ($_FILES["file"]["error"] > 0)
{
echo "Error: " . $_FILES["file"]["error"] . "<br>";
}
if (isset($_FILES['picture']) && file_exists($_FILES['picture']['tmp_name']))
{
$image = $_FILES['picture']['tmp_name'];
//~ Check if image is an image
if (#getimagesize($image))
{
$upload_status = TRUE;
//~ from here you can use yours image as $_FILES['picture']['name'], for example to copy it
move_uploaded_file($image, realpath(dirname(__FILE__)).'/images/'.$_FILES['picture']['name']);
//~ Also be noticed that the image curently is in OS tmp folder and if you dont copy it, it will be deleted after script execution.
}
}
if ($upload_status)
{
echo 'Image successfully uploaded. <br> <img src="images/'.$_FILES['picture']['name'].'">';
}
else
{
echo 'nope.jpg';
}
?>
The output is always;
picture not set
nope.jpg
This means that $_Files['picture'] is not set, and there are no errors in the files array.
As you can see from the code above i have already tried the following fixes;
Added the markup for form enctype; enctype="multipart/form-data"
Added a hidden MAX_FILE_SIZE attribute
Not show in the code, i have tried adding size='30000000' in the file tag
I've checked that the value / name are the same when setting and getting file
I've also checked php.ini to ensure that file_upload is allowed
What could I possibly be missing?
Edit; I've tried this on my desktop and mobile browsers.
I found the solution to actually be a problem between the obvious (Not being able to file upload using ajax), and jquerymobile framework, which uses ajax on it's forms by default.
To fix the problem I added data-ajax='false'
<form enctype="multipart/form-data" action="decodeQR.php" method="post" data-ajax='false'>
The file upload works fine, so i'm posting this answer for anyone who's using jquerymobile and comes across this problem! : )
Have you checked if the request sent by the browser contains the file?
BTW. I'm new here. How do you guys add these "comments" to questions?
Ok so im making an image uploader with a chance to see what you are going to upload, before entering it into the database. My mind is kinda tired, could you please just get me through this one? thx (btw it doesnt work :p) Its on a clean page, i choose a file from my pictures on compouter, it doesn echo "hi wazzup
<?php
if(isset($_FILES['image'])){
echo "hi wazzup!";
}
?>
<form name="form" id="form" method="post" enctype="multipart/form-data" action="<?php echo $_SERVER['PHP_SELF']; ?>" >
<input type="file" name="image" onChange="if(event.propertyName=='value') {document.getElementById("form").submit();}">
</form>
See what is the output and you will know wheater your javascript is wrong or you should use $_FILES instead of $_POST for image
<?php
if(isset($_POST))
{
if(isset($_FILES['image'])){
echo "<span style=\"z-index:1000;display:block;\">hi wazzup!</span>";
}
echo "POSTED";
}
?>