How to make a folder PHP

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
roma2509
Forum Newbie
Posts: 16
Joined: Fri Sep 07, 2012 6:20 pm

How to make a folder PHP

Post by roma2509 »

Hi. I am trying to create a folder after submit button. My cod is:

Code: Select all

$dirPath = "../uploads/".$_GET['title'];
$result = mkdir($dirPath, 0755);
It is a form for ading data in to database. After submit button All data are inserted in data base. But I want to create a folder with the name of variable "title". But it not create anything. What is wrong in my cod?
All of my coding you can see:

Code: Select all

<?php
/**
 *
 * eliteCMS - The Lightweight CMS Copyright © 2008 elite-graphix.net.
 * eliteCMS is a free software to use for personal websites.
 * Anyone can use eliteCMS for free as long as he retain the
 * copyright note at the footer.
 * If anyone wants to remove the copyright note for commercial
 * use of the software can contact raj@elite-graphix.net.
 * eliteCMS comes with absolutely no warranty and is provided as-is.
 * raj - THE AUTHOR of the software or elite-graphix - THE COMPANY
 * will not be held responsible for any damage this software might cause.
 * You run this software at your own risk and agree not to hold author or company.
 * You are not allowed to make any profit from this software unless you
 * have a written authorization from the author to use the software for commercial use.
 *
 */
include ("includes/headerRefresh.php");
include ("includes/config.php");
include ("functions/functions.php");
require_once ("includes/session.php");
check_login();
get_settings();
include ("header.php");

$errors = array();
if (isset($_POST['submit'])) {

    if ($_POST['title'] == "")
        $errors['title'] = "Title of the Post is required !";

    

    if ($_POST['content'] == "")
        $errors['content'] = "Content of the Post is required !";


    if (empty($errors)) {

        $page_id = mysql_prep($_POST['page_id']);
        $title = mysql_prep($_POST['title']);
        $active = mysql_prep($_POST['active']);
        $position = mysql_prep($_POST['position']);
        $content = mysql_prep($_POST['content']);
		$address = mysql_prep($_POST['address']);
		$phone = mysql_prep($_POST['phone']);
		$web = mysql_prep($_POST['web']);
		$email = mysql_prep($_POST['email']);
		$video = mysql_prep($_POST['video']);
		$map = mysql_prep($_POST['map']);
		$valid = mysql_prep($_POST['valid']);

        $query = "INSERT INTO posts (page_id, title, active, position, content, address, phone, web, email, video, map, valid) VALUES ('{$page_id}', '{$title}', '{$active}', '{$position}', '{$content}', '{$address}', '{$phone}', '{$web}', '{$email}', '{$video}', '{$map}', '{$valid}')";
        $result = mysql_query($query);
        confirm_query($result);
        if ($result) {
            $successMSG = "Post Created Successfully !";
            redirect_to("manage_posts.php?newpost=1");

        } else {
            $errorMSG = "Post Creation Failed ! </br>" . mysql_error() . " ";

        }


    } else {
        $displayError = '<div class="errorMSG"><ul style="list-style:none;">';

        foreach ($errors as $error) {
            $displayError .= "<li>$error</li>";
        }
        $displayError .= '</ul></div>';
    }
$dirPath = "../uploads/".$_GET['title'];
$result = mkdir($dirPath, 0755);

} else {
    //$errorMSG = "";
}
?>
<?php
if (isset($_GET['new']) && $_GET['new'] == 1) {
    $successMSG = "Page created successfully. Now lets add some posts to it !.";

}
?>

<div id="body">
<div class="box bigBox">
<h1>Add New Post</h1>
<?php display_msg(); ?>
<?php selected_page(); ?>
<form action="<?php echo $PHP_SELF; ?>" method="post">
<table width="100%" align="center" cellpadding="0" cellspacing="0" id="post_form">
<tr bgcolor="#EEF7FD">
<td width="27%" class="padd">Categoria Companie :</td>
<td width="73%" class="padd">
<select name="page_id" class="select1" onChange="MM_jumpMenu('parent',this,0)">
<?php
if (isset($selected_page['id'])) {
    echo "<option value=\"{$selected_page['id']}\">{$selected_page['menu_name']}</option>\n";
} else {
    $query = "SELECT * FROM pages ORDER BY id";
    $result = mysql_query($query);
    confirm_query($result);
    while ($pages = mysql_fetch_array($result)) {
        echo "<option value=\"add_post.php?page={$pages['id']}\">{$pages['menu_name']}</option>\n";
    }
}
?>
</select>
</td>
</tr>
<tr>
<td class="padd">Denumire Companie :</td>
<td class="padd">
<input name="title" type="text" class="input" id="title" value="<?php echo $_POST['title'] ?>"/>
<?php if (!empty($errors['title'])) {
    echo "&nbsp;<span class=\"mend\">*</span>";
} ?>
</td>
</tr>
<tr bgcolor="#EEF7FD">
<td class="padd">Post Published :</td>
<td class="padd">
<select name="active" class="select">
<option value="1">Yes</option>
<option value="0">No</option>
</select>
</td>
</tr>
<tr>
<td valign="bottom" class="padd">Post Position :</td>
<td valign="bottom" class="padd">
<?php if (isset($_GET['page'])) {
    display_acquired_posts();

} ?>
<input name="position" type="text" class="inputSmall" id="position" value="<?php echo
$_POST['position'] ?>"/>
<?php if (!empty($errors['position'])) {
    echo "&nbsp;<span class=\"mend\">*</span>";
} ?>
</td>
</tr>
<tr bgcolor="#EEF7FD">
<td class="padd">Descriere Companie</td>
<td class="padd">
<script language="JavaScript1.2" type="text/javascript">
  WYSIWYG.attach('content', full);
  </script>
<textarea name="content" id="content" class="contentArea"><?php echo $_POST['content'] ?></textarea>
<?php if (!empty($errors['content'])) {
    echo "&nbsp;<span class=\"mend\">*</span>";
} ?>
</td>
</tr>
<tr>
<td class="padd">Adresa Companie :</td>
<td class="padd">
<input name="address" type="text" class="input" id="address" value="<?php echo $_POST['address'] ?>"/>
<?php if (!empty($errors['address'])) {
    echo "&nbsp;<span class=\"mend\">*</span>";
} ?>
</td>
</tr>

<tr>
<td class="padd">Nr. Telefon Companie :</td>
<td class="padd">
<input name="phone" type="text" class="input" id="phone" value="<?php echo $_POST['phone'] ?>"/>
<?php if (!empty($errors['phone'])) {
    echo "&nbsp;<span class=\"mend\">*</span>";
} ?>
</td>
</tr>

<tr>
<td class="padd">Adresa Web :</td>
<td class="padd">
<input name="web" type="text" class="input" id="web" value="<?php echo $_POST['web'] ?>"/>
<?php if (!empty($errors['web'])) {
    echo "&nbsp;<span class=\"mend\">*</span>";
} ?>
</td>
</tr>

<tr>
<td class="padd">Email Companie :</td>
<td class="padd">
<input name="email" type="text" class="input" id="email" value="<?php echo $_POST['email'] ?>"/>
<?php if (!empty($errors['email'])) {
    echo "&nbsp;<span class=\"mend\">*</span>";
} ?>
</td>
</tr>

<tr>
<td class="padd">Video Link :</td>
<td class="padd">
<input name="video" type="text" class="input" id="video" value="<?php echo $_POST['video'] ?>"/>
<?php if (!empty($errors['video'])) {
    echo "&nbsp;<span class=\"mend\">*</span>";
} ?>
</td>
</tr>

<tr>
<td class="padd">Harta Companie :</td>
<td class="padd">
<input name="map" type="text" class="input" id="map" value="<?php echo $_POST['map'] ?>"/>
<?php if (!empty($errors['map'])) {
    echo "&nbsp;<span class=\"mend\">*</span>";
} ?>
</td>
</tr>

<tr>
<td class="padd">Activ pina la: </td>
<td class="padd">
<input name="valid" type="text" class="input" id="valid" value="<?php echo $_POST['valid'] ?>"/>
<?php if (!empty($errors['valid'])) {
    echo "&nbsp;<span class=\"mend\">*</span>";
} ?>
</td>
</tr>



<tr>
<td>&nbsp;</td>
<td class="padd">
<input type="submit" name="submit" id="submit" value="Add Post"  class="formBU"/>
<input name="reset" type="reset" class="formBU" value="Reset Form" />
<a href="manage_posts.php" target="_self" class="linkBU">Cancel</a></td>
</tr>
</table>
</form>
</div>
</div>
<?php include ("footer.php") ?>
User avatar
requinix
Spammer :|
Posts: 6617
Joined: Wed Oct 15, 2008 2:35 am
Location: WA, USA

Re: How to make a folder PHP

Post by requinix »

Your code is fine. Your idea that you have to create folders according to user-submitted information is what's wrong. Why do you think you need to do that?
roma2509
Forum Newbie
Posts: 16
Joined: Fri Sep 07, 2012 6:20 pm

Re: How to make a folder PHP

Post by roma2509 »

Idea is that is for admin zone(back-end). After admin submit button, the page will redirect to other page where admin will can upload images in this created folder. The folder will take the name of product. The problem is that my scritpt don't create anything and I can not understand why so?
User avatar
requinix
Spammer :|
Posts: 6617
Joined: Wed Oct 15, 2008 2:35 am
Location: WA, USA

Re: How to make a folder PHP

Post by requinix »

Okay, that sounds like a legitimate reason for doing this. Normally you shouldn't but in this case it makes sense.

Check the permissions on the parent folder - the one where you are creating the new folder. It needs to be 0777 (read, write, and execute permissions for everybody). That's because you, the user who uploaded files to the server, is not the same user as PHP, the one trying to create the directories.
roma2509
Forum Newbie
Posts: 16
Joined: Fri Sep 07, 2012 6:20 pm

Re: How to make a folder PHP

Post by roma2509 »

I think that problem is not in this thing, if I dont attribute any variable on creating folder the script create the new folder with the name that I will give it. For example I use this functions

Code: Select all

$dirPath = "../uploads/".$_GET['title'];
$result = mkdir($dirPath, 0777);
and the script dont do anything, but if I will give a constant name instead of .$_GET['title'] it will create the directory, for example if I use

Code: Select all

mkdir("../uploads/some_name");

An I can not understand where is the problem.
User avatar
requinix
Spammer :|
Posts: 6617
Joined: Wed Oct 15, 2008 2:35 am
Location: WA, USA

Re: How to make a folder PHP

Post by requinix »

If the only difference is that the broken code uses $_GET["title"] then maybe the problem is in $_GET["title"].

Should it perhaps be $_POST["title"]?
roma2509
Forum Newbie
Posts: 16
Joined: Fri Sep 07, 2012 6:20 pm

Re: How to make a folder PHP

Post by roma2509 »

Thank you for the answer. It all ok :)
Post Reply