Jump to content

[SOLVED] URL of image into database


garydt

Recommended Posts

I can upload an image and copy it into a folder and i'm trying to put the url into a database. The first half of the url goes in but the filename goes in still as as variable, eg  C:/Program Files/xampp/htdocs/epeople/uploads/$uploadFilename

 

The code-

<?php require_once('Connections/elvisdb.php'); ?>

<?php

 

 

// filename: upload.processor.php

 

// first let's set some variables

 

// make a note of the current working directory, relative to root.

$directory_self = str_replace(basename($_SERVER['PHP_SELF']), '', $_SERVER['PHP_SELF']);

 

// make a note of the directory that will recieve the uploaded file

$uploadsDirectory = $_SERVER['DOCUMENT_ROOT'] . $directory_self . 'uploads/';

 

// make a note of the location of the upload form in case we need it

$uploadForm = 'http://' . $_SERVER['HTTP_HOST'] . $directory_self . 'uploadform.php';

 

// make a note of the location of the success page

$uploadSuccess = 'http://' . $_SERVER['HTTP_HOST'] . $directory_self . 'uploadsucs.php';

 

// fieldname used within the file <input> of the HTML form

$fieldname = 'file';

// Now let's deal with the upload

 

// validation... since this is an image upload script we should run a check 

// to make sure the uploaded file is in fact an image. Here is a simple check:

// getimagesize() returns false if the file tested is not an image.

@getimagesize($_FILES[$fieldname]['tmp_name']);

   

// make a unique filename for the uploaded file and check it is not already

// taken... if it is already taken keep trying until we find a vacant one

// sample filename: 1140732936-filename.jpg

$now = time();

while(file_exists($uploadFilename = $uploadsDirectory.$now.'-'.$_FILES[$fieldname]['name']))

{

    $now++;

}

 

 

// now let's move the file to its final location and allocate the new filename to it

@move_uploaded_file($_FILES[$fieldname]['tmp_name'], $uploadFilename);

 

$pathtoimg = $_SERVER['DOCUMENT_ROOT'] . $directory_self . 'uploads/'.'$uploadFilename';

session_start();

 

$user = $_SESSION['MM_Username'];

 

mysql_select_db($database_elvisdb, $elvisdb);

$query = sprintf("INSERT INTO images (imageName, usnm) VALUES ('$pathtoimg', '$user')");

mysql_query($query);

 

echo $fieldname;

echo $pathtoimg;

echo $user

?>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">

<head>

<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />

<title>Untitled Document</title>

</head>

 

<body>

<form name="form1" id="Upload" action="<?php echo $editFormAction; ?>" enctype="multipart/form-data" method="POST">

   

        <h1>

            Upload form

        </h1>

       

        <p>

            <input type="hidden" name="MAX_FILE_SIZE" value="<?php echo $max_file_size ?>">

        </p>

       

        <p>

            <label for="file">File to upload:</label>

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

        </p>

        <p>

          <label>

          <input type="text" name="textfield" />

          </label>

        </p>

        <p>

            <label for="submit">Press to...</label>

            <input id="submit" type="submit" name="submit" value="Upload me!">

        </p>

   

        <input type="hidden" name="MM_insert" value="form1">

</form>

</body>

</html>

 

Link to comment
https://forums.phpfreaks.com/topic/43115-solved-url-of-image-into-database/
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.