Jump to content

Uploading images into a specific area


QuizToon

Recommended Posts

Hi All

 

I want to upload some information and an image to a database, and then retrieve the information into a web page.  Simple enough to do I know but I am struggling with some specifics.  I am using the following to upload the information

 

<?php 

$uploadDir = 'pictures/'; 
$fileUploaded = 0; // set a flag for use later

// connection parameters should be in a seperate file!
include('/path/to/site/folder/config.php');

$database = mysql_connect($hostname,$username,$password) or die("Could not connect to server");
mysql_select_db($table,$database) or die("Could not find table");

//$conn = mysql_connect($dbhost, $dbuser, $dbpass) or die ('Error connecting to mysql');
//mysql_select_db($dbname); 
////////////////////////////////////

if(isset($_POST['upload'])) 
{ 

$id = $row['id']; 
$title = addslashes($_POST['title']);
$detail = addslashes($_POST['detail']);

// check if a file has been uploaded
if($_FILES['userfile']['error'] != 4) { // error code 4 meaning that no image is present
    $fileUploaded = 1;
    $fileName = $_FILES['userfile']['name']; 
    $tmpName  = $_FILES['userfile']['tmp_name']; 
    $fileSize = $_FILES['userfile']['size']; 
    $fileType = $_FILES['userfile']['type'];
    
    // the files will be saved in filePath  
    $filePath = $uploadDir . $fileName; 

    // move the files to the specified directory 
    // if the upload directory is not writable or 
    // something else went wrong $result will be false 
    $result    = move_uploaded_file($tmpName, $filePath);

    if (!$result) { 
        echo "Error uploading file";
        exit; 
    } 
} else {
     // if no file added, generate empty variables so as not to break the query
    $fileName = '';
    $fileSize = '';
    $fileType = '';
    $filePath = '';
}

    if(!get_magic_quotes_gpc()) 
    { 
        $fileName  = addslashes($fileName); 
        $filePath  = addslashes($filePath); 
        
    }   

    $query = "INSERT INTO lindablog (name, size, type, path, title, detail) ". 
             "VALUES ('$fileName', '$fileSize', '$fileType', '$filePath', '$title', '$detail')"; 

    mysql_query($query) or die('Error, query failed : ' . mysql_error());                     

    mysql_close($database);
    
    // if a file was uploaded, the fileUploaded variable will be set to 1
    if($fileUploaded == 1) {
        echo "<br>File uploaded<br>";
	// show the image after upload - comment this out if not required - good for checking though 
        echo '<img src="'. $uploadDir .'/'.$_FILES['userfile']['name'][$i] .'" >'; 
        $movedtolocation="pictures/".$_FILES['upload']['name'];# basically, take the location of wher the image is stored.. 
    }

} 
?>
<link href="styleadmin.css" rel="stylesheet" type="text/css">


<form action="" method="post" enctype="multipart/form-data" name="uploadform"> 
  <table width="609" align="center" cellpadding="1" cellspacing="1"> 
    <tr>
      <td colspan="4" align="center"> </td>
    </tr>
    <tr>  
      <td width="165"><p class="subtitle">Main Title :</p></td>
  <td width="437" colspan="3"><p> <input name="title" type="text" size="35"> </td>
    </tr>
  <tr>
    <td width="165" valign="top"> </td>
    <td colspan="3"> </td>
    </tr>
  <tr>
    <td width="165" valign="top"><p class="subtitle"> Blog :</p>
	<p align="center" class="small"> </p></td>
    <td colspan="3"><textarea name="detail" id="detail" cols="50" rows="20"></textarea></td>
    </tr>
      <tr>
	<td width="165"><p class="subtitle">Select Picture:</p></td>
	<td colspan="3"><p align="center">
	  <input type="hidden" name="MAX_FILE_SIZE" value="2000000">
	  <input name="userfile" type="file" class="box" id="userfile"  size="35">
	  <span class="small">    <br>
	  Leave blank if no picture to upload</span><br>
      <br>
	</p></td>
      <tr>
        <td width="165"> </td>
        <td colspan="3"> </td>
      </tr>
      <tr><td colspan="4" align="center"><input name="upload" type="submit" class="box" id="upload" value="  Upload  "></td> 
      </tr> 
  </table> 
</form>

 

All of this works fine.  Now the admin file which does all this is in a directory i want to password protect, when I do this it also protects the dirtectory to which the image was uploaded.  How do I either upload the image toa directory outside the admin directory, or how can i call the picture without having to input the password, as when the webpage loads and it tries to display the image it asks for a password.

 

If we can upload the image outside of the protected directory how do I reference it in the webpage.

 

Hope you understand all this

 

Thanks in advance

 

 

Link to comment
Share on other sites

If this .php file is in the admin folder, i assume the structure looks like this:

 

admin > pictures

 

You can use

$uploadDir = '../pictures/'; 

and it will upload it into a folder along side admin:

 

admin

pictures

 

As for calling the pictures in html...

 

<img src="site.com/pictures/picturename.jpg>

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.