• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 286
  • Last Modified:

PHP Save Directory for Image

What code do I use to target the directory, I used something similar to what is written below and it was fine.

The image is in: image/

Please tell me what function do I need to store that file in the directory?
$target = "image/"; 
			
//Writes the photo to the server 
			
if(move_uploaded_file($_FILES['Pic']['tmp_name'], $target))

Open in new window

0
namsu55
Asked:
namsu55
  • 4
  • 4
1 Solution
 
albrieuCommented:
you most to create a dir after

with

Example #1 mkdir() example
<?php
if(!is_dir($target) and file)
mkdir($targett, 0700);
?>



<?php
if (!file_exists($target) and !is_dir($target)) 
{
   mkdir($target, 0700);
}
?>
												

Open in new window

0
 
namsu55Author Commented:
I already have that directory created.
0
 
viral_sonawalaCommented:
Try this code and create new folder as per this path C:/webroot/upload/ in your computer.
<?php
     $uploadDir = 'C:/webroot/upload/';
     
     if(isset($_POST['upload']))
     {
     $fileName = $_FILES['userfile']['name'];
     $tmpName = $_FILES['userfile']['tmp_name'];
     $fileSize = $_FILES['userfile']['size'];
     $fileType = $_FILES['userfile']['type'];
 $filePath = $uploadDir . $fileName;
 $result = move_uploaded_file($tmpName, $filePath);
     if (!$result) {
     echo "Error uploading file";
     exit;
     }
     
     include '../library/config.php';
     include '../library/opendb.php';
 if(!get_magic_quotes_gpc())
     {
     $fileName = addslashes($fileName);
     $filePath = addslashes($filePath);
     }
 $query = "INSERT INTO upload2 (name, size, type, path                     ) ".
     "VALUES ('$fileName', '$fileSize', '$fileType', '$filePath')";
 mysql_query($query) or die('Error, query failed : ' . mysql_error());    
 include '../library/closedb.php';
     
     echo "<br>Files uploaded<br>";
}
     ?>
<?php
      $uploadDir = 'C:/webroot/upload/';
     
      if(isset($_POST['upload']))
      {
      $fileName = $_FILES['userfile']['name'];
      $tmpName = $_FILES['userfile']['tmp_name'];
      $fileSize = $_FILES['userfile']['size'];
      $fileType = $_FILES['userfile']['type'];
 $filePath = $uploadDir . $fileName;
 $result = move_uploaded_file($tmpName, $filePath);
      if (!$result) {
      echo "Error uploading file";
      exit;
      }
     
      include '../library/config.php';
      include '../library/opendb.php';
 if(!get_magic_quotes_gpc())
      {
      $fileName = addslashes($fileName);
      $filePath = addslashes($filePath);
      }  
 $query = "INSERT INTO upload2 (name, size, type, path                      ) ".
      "VALUES ('$fileName', '$fileSize', '$fileType', '$filePath')";
 mysql_query($query) or die('Error, query failed : ' . mysql_error());      
 include '../library/closedb.php';
     
      echo "<br>Files uploaded<br>";
}
      ?>
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
namsu55Author Commented:
I have this:
<?  
  $uploadDir = 'image/';
   
     if(isset($_POST['registration']))
    {
    $fileName = $_FILES['userfile']['name'];
    $tmpName = $_FILES['userfile']['tmp_name'];
    $fileSize = $_FILES['userfile']['size'];
    $fileType = $_FILES['userfile']['type'];
 
$filePath = $uploadDir . $fileName;
 
$result = move_uploaded_file($tmpName, $filePath);
    if (!$result) {
    echo "Error uploading file";
    exit;
    }
	}
?>
 
<html>
<body>
 
<form onsubmit="return validateFormOnSubmit(this)" action="<?= $_SERVER['PHP_SELF'] ?>" method="post" name="registration">
<input name="Pic" type="file" id="Pic">
<input name="BtnAdd" type="submit" id="BtnAdd" value="Add Item">   
 <input type="hidden" name="submitcheck" value="1">
</form>
</body>
</html>

Open in new window

0
 
namsu55Author Commented:
That doesnt work. HTML form works fine, add a picture, nothing comes up in my directory, I am working on a server, I done this yesterday but I lost the code because wrote over to try something else.
0
 
albrieuCommented:
the script and the directory is in same place?
0
 
namsu55Author Commented:
albrieu: YES.
0
 
albrieuCommented:
try this code
<?  
  $uploadDir = 'image/';
   
     if(isset($_POST['registration']))
    {
    $fileName = $_FILES['Pic']['name'];
    $tmpName = $_FILES['Pic']['tmp_name'];
    $fileSize = $_FILES['Pic']['size'];
    $fileType = $_FILES['Pic']['type'];
 
$filePath = $uploadDir . $fileName;
 
$result = move_uploaded_file($tmpName, $filePath);
    if (!$result) {
    echo "Error uploading file";
    exit;
    }
        }
?>
 
<html>
<body>
 
<form onsubmit="return validateFormOnSubmit(this)" action="<?= $_SERVER['PHP_SELF'] ?>" method="post" name="registration" enctype="multipart/form-data">
<input name="MAX_FILE_SIZE" value="6000000000" type="hidden">
<input name="Pic" type="file" id="Pic">
<input name="BtnAdd" type="submit" id="BtnAdd" value="Add Item">   
 <input type="hidden" name="submitcheck" value="1">
 <input type="hidden" name="registration" value="1">
</form>
</body>
</html>

Open in new window

0
 
albrieuCommented:
in form add:

enctype="multipart/form-data"

<input name="MAX_FILE_SIZE" value="6000000000" type="hidden">

and

<input type="hidden" name="registration" value="1">
 
to check the post with:

  if(isset($_POST['registration']))

and

$_FILES['Pic'] not userfile the name most be same at input tag

<input name="Pic" type="file" id="Pic">

bye


   
0

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

  • 4
  • 4
Tackle projects and never again get stuck behind a technical roadblock.
Join Now