Jump to content

Recommended Posts

I have this code on my index.php page where i want the image to show;

 

index.php

<?php

require_once('db.inc.php');

$q = mysql_query("SELECT * FROM events ORDER BY `date` DESC");

if (mysql_num_rows($q) > 0) {

  while ($rec = mysql_fetch_array($q)) {

    echo "<div class=\"news_entry\">\n";

    echo "<a href=\"latest-events.php?id=$rec[id]\"><h1>$rec[title]</h1></a>\n";    echo !empty($rec['image']) ? "<a href=\"latest-events.php?id=$rec[id]\"><img src=\"library/files/$rec[image]\" /></a>\n" : '';

echo "<p>" . nl2br(substr($rec['content'], 0 ,250)) . "</p>\n";

    echo "<p>Posted " . date('F j, Y', strtotime($rec['date'])) . "</p>\n";

    echo "</div>\n";

  }

}

?>

 

and i have this where im trying to upload the image from;

 

cms-events.php

 

<html>

<head>

<title>Add An Article</title>

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

<style type="text/css">

<!--

.box {

font-family: Arial, Helvetica, sans-serif;

font-size: 12px;

border: 1px solid #000000;

}

-->

</style>

</head>

 

<body>

<?php

require_once('db.inc.php');

if ($_POST)

{

$title = $_POST['title'];

$date = $_POST['date'];

$content = $_POST['content'];

$image = $_POST['image'];

 

if(!get_magic_quotes_gpc())

{

$title = addslashes($title);

$date = addslashes ($date);

$content = addslashes($content);

$image = $_FILES['imagefile']['name'];

}

 

 

$query = "INSERT INTO events (title, date, content, image) VALUES ('$title', 'date', '$content', '$image')";

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

 

 

echo "Article '$title' added";

}

?>

<form method="post" action="cms-events.php" enctype="multipart/form-data">

<table width="700" border="0" cellpadding="2" cellspacing="1" class="box" align="center">

<tr>

<td width="100">Title</td>

<td><input name="title" type="text" class="box" id="title"></td>

</tr>

<tr>

  <td>Date</td>

  <td><input name="date" type="text" class="box" id="date"></td>

</tr>

<tr>

<td width="100">Content</td>

<td><textarea name="content" cols="50" rows="10" class="box" id="content"></textarea></td>

</tr>

<tr>

<td width="100"> </td>

<td><input type="file" name="imagefile">

<br>

<input type="submit" name="Submit" value="Submit">

<?php

if(isset( $Submit ))

{

//If the Submitbutton was pressed do:

 

if ($_FILES['imagefile']['type'] == "image/jpg" || $_FILES['imagefile']['type'] == "image/jpeg"){ //<--- Note the type (check the change below and post the error)

move_uploaded_file($_FILES['imagefile']['tmp_name'], "files/".$_FILES['imagefile']['name'])or die ("Could not copy");

echo "";

echo "Name: ".$_FILES['imagefile']['name']."";

echo "Size: ".$_FILES['imagefile']['size']."";

echo "Type: ".$_FILES['imagefile']['type']."";

echo "Stored in: " . $_FILES["file"]["files"];

echo "Copy Done....";

}

 

else {

echo "<br><br>";

echo "Could Not Copy, Wrong Filetype (".$_FILES['imagefile']['type'].")<br>"; //<--Note change from name to type

}

}

 

?></td>

</tr>

<tr>

<td colspan="2" align="center"><input name="save" type="submit" class="box" id="save" value="Save Article"></td>

</tr>

</table>

</form>

</body>

</html>

 

 

At the moment its working perfectly, and the file is uploading to my ftp server under /files folder but the image is not displaying on my index.php page?

 

When i than go to the database to check all fields have been entered apart from the image field? is this why? does it also need to go the database before it can be viewed on the index.php page? or do i have to add an extra line of code for the image to display on the index.php page...

Link to comment
https://forums.phpfreaks.com/topic/53510-image-upload-99-there/
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.