We help IT Professionals succeed at work.

We've partnered with Certified Experts, Carl Webster and Richard Faulkner, to bring you two Citrix podcasts. Learn about 2020 trends and get answers to your biggest Citrix questions!Listen Now

x

PHP Save Directory for Image

namsu55
namsu55 asked
on
Medium Priority
296 Views
Last Modified: 2013-12-12
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

Comment
Watch Question

Commented:
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

Author

Commented:
I already have that directory created.
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>";
}
      ?>

Author

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

Author

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.

Commented:
the script and the directory is in same place?

Author

Commented:
albrieu: YES.
Commented:
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

Not the solution you were looking for? Getting a personalized solution is easy.

Ask the Experts

Commented:
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


   
Access more of Experts Exchange with a free account
Thanks for using Experts Exchange.

Create a free account to continue.

Limited access with a free account allows you to:

  • View three pieces of content (articles, solutions, posts, and videos)
  • Ask the experts questions (counted toward content limit)
  • Customize your dashboard and profile

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.