5004Views11Replies

Author Options:

How to create a new folder and text file in php.? Answered

I want to create a new folder and a text file while uploading image. I can upload picture but cannot able to create a new folder where uploaded picture directly upload to a new folder. Can anyone help me?


<?php
//define a maxim size for the uploaded images in Kb
define ("MAX_SIZE","100000");

//This function reads the extension of the file. It is used to determine if the file is an image by checking the extension.
function getExtension($str) {
$i = strrpos($str,".");
if (!$i) { return ""; }
$l = strlen($str) - $i;
$ext = substr($str,$i+1,$l);
return $ext;
}

//This variable is used as a flag. The value is initialized with 0 (meaning no error found)
//and it will be changed to 1 if an errro occures.
//If the error occures the file will not be uploaded.
$errors=0;
//checks if the form has been submitted
if(isset($_POST['Submit']))
{
//reads the name of the file the user submitted for uploading
$image=$_FILES['image']['name'];
//if it is not empty
if ($image)
{
//get the original name of the file from the clients machine
$filename = stripslashes($_FILES['image']['name']);
//get the extension of the file in a lower case format
$extension = getExtension($filename);
$extension = strtolower($extension);
//if it is not a known extension, we will suppose it is an error and will not upload the file,
//otherwise we will do more tests
if (($extension != "jpg") && ($extension != "jpeg") && ($extension != "png") && ($extension != "gif"))
{
//print error message
echo '<h1>Unknown extension!</h1>';
$errors=1;
}
else
{
//get the size of the image in bytes
//$_FILES['image']['tmp_name'] is the temporary filename of the file
//in which the uploaded file was stored on the server
$size=filesize($_FILES['image']['tmp_name']);

//compare the size with the maxim size we defined and print error if bigger
if ($size > MAX_SIZE*10024)
{
echo '<h1>You have exceeded the size limit!</h1>';
$errors=1;
}

//we will give an unique name, for example the time in unix time format
$image_name=time().'.'.$extension;
//the new name will be containing the full path where will be stored (images folder)
$newname="images/".$image_name;
//we verify if the image has been uploaded, and print error instead
$copied = copy($_FILES['image']['tmp_name'], $newname);
if (!$copied)
{
echo '<h1>Copy unsuccessfull!</h1>';
$errors=1;
}}}}

//If no errors registred, print the success message
if(isset($_POST['Submit']) && !$errors)
{
echo "<h1>File Uploaded Successfully!</h1>";
echo "$image_name";

}



?>

Tags:php

Discussions

0
None
Jayefuu

7 years ago

At least your code is well commented!

Where in that code do you attempt to make a new folder? I see no reference to the function mkdir....

http://php.net/manual/en/function.mkdir.php

If you've tried to use it in your script and failed, have you written a separate script with the sole purpose of creating a folder? When debugging start simple, split everything apart and find out what works and what doesn't.

0
None
nazarkhanJayefuu

Answer 7 years ago

Thanks Jayefuu for the prompt response.

In fact I am new to php. I am trying using mkdir code but it's giving me an error.

Please help me.

0
None
Jayefuunazarkhan

Answer 7 years ago

What's the error?

Get rid of all of the gumpf in the code above and start anew. Make a script that JUST creates a directory. That's as simple as you can make it. Does it work? If not, what is the error?

0
None
JayefuuJayefuu

Answer 7 years ago

Oh, and since it's making folders, have you checked that it has permission to add folders?

If you're using PHP locally you shouldn't have a problem. If it's on a web server somewhere, check the permissions, I use Filezilla as my FTP client, you can browse to a folder and right click and then click "File Permissions"

0
None
nazarkhanJayefuu

Answer 7 years ago

I am using Cute Ftp and i have checked the permission as well.

I will tell actually what I am looking for. Please visit.

http://www.nazarkhan.com/abc

I need anyone upload picture on the server without any login and will create a folder in images folder with a picture.

Right now it saves in images folder but not creating a folder inside.
I have inserted mkdir code but its giving me an error but picture uploaded successfully

Warning: mkdir() [function.mkdir]: File exists in /nfs/c04/h04/mnt/80175/domains/nazarkhan.com/html/abc/index.php on line 154

my code is:
//get the size of the image in bytes
//$_FILES['image']['tmp_name'] is the temporary filename of the file
//in which the uploaded file was stored on the server
$size=filesize($_FILES['image']['tmp_name']);

//compare the size with the maxim size we defined and print error if bigger
if ($size > MAX_SIZE*10024)
{
echo '

You have exceeded the size limit!

';
$errors=1;
}

//we will give an unique name, for example the time in unix time format

mkdir("images", 0700);


$image_name=time().'.'.$extension;
//the new name will be containing the full path where will be stored (images folder)
$newname="images/".$image_name;
//we verify if the image has been uploaded, and print error instead

$copied = copy($_FILES['image']['tmp_name'], $newname);
if (!$copied)
{
echo '

Copy unsuccessfull!

';
$errors=1;
}}}}

//If no errors registred, print the success message
if(isset($_POST['Submit']) && !$errors)
{
echo "

File Uploaded Successfully!

";
echo "$image_name";

}

?>

0
None
Jayefuunazarkhan

Answer 7 years ago

That's not an error, it's a warning. It doesn't think it needs to do it, because the folder already exists. Do you have a directory called images? You said you were going to name it something unique...

Try these, I've never used PHP's mkdir function but this is what I'd look for:

1) Is your path correct?
2) If you're not sure your path is correct, can you use a different function to put your current path into a variable?
3) Did you try as I suggested and create a separate script that JUST creates a directory? Get that working then use what you learn in doing so to make your other script work.
4) Is your path unique?

0
None
nazarkhanJayefuu

Answer 7 years ago

Thanks Buddy.

Now I can able to create an folder with unique file name but picture is saving on the root. I am trying to save picture in the folder.


$dirPath = "images/$image_name";
$copied = mkdir ("images/$image_name", 777);
if ($result == 1) {
echo "images/$image_name". " has been created";
} else {
echo "images/$image_name". " has NOT been created";
}


//we verify if the image has been uploaded, and print error instead

$copied = copy($_FILES['image']['tmp_name'], $image_name);
if (!$copied)
{
echo '

Copy unsuccessfull!

';
$errors=1;
}}}}

0
None
Jayefuunazarkhan

Answer 7 years ago

Shouldn't:
$copied = mkdir ("images/$image_name", 777);

be:
$result = mkdir ("images/$image_name", 777);

Other than that, you've not included the code that saves the picture

0
None
nazarkhanJayefuu

Answer 7 years ago

Could you please help me and correct my coding
I am using this code saving picture but doesn't save in the specific folder it's save on the root folder
$copied = copy($_FILES['image']['tmp_name'], $image_name);

Please help!

0
None
Jayefuunazarkhan

Answer 7 years ago

You need to prepend the name of the folder to $image_name.

So:
$image_name = $image_name."/".$image_name;
$copied = copy($_FILES['image']['tmp_name'], $image_name);

This copies the image to the new folder you just made. Right?

0
None
nazarkhanJayefuu

Answer 7 years ago

Boss,Still I can't figure out how to save picture in the folder :(

ïmages/image_name

$dirPath = "images/$image_name";
$result = mkdir ("images/$image_name", 777);
if ($result == 1) {
echo "images/$image_name". " has been created";
} else {
echo "images/$image_name". " has NOT been created";
}