J'ai des problèmes avec le code de téléchargement de ce script, je l'ai modifié et ajouté d'autres fonctionnalités qui fonctionnent. Juste la partie téléchargement du script ne fonctionne pas, je vais fournir le code complet pour tous les fichiers.problème de php de téléchargement de fichier ou d'image
upload.php
<?php
require_once 'dbc.php';
page_protect();
$client_ID = mysql_query("SELECT id
FROM clients WHERE user_name='".$_SESSION['user_name']."'")or die(mysql_error());
$client_ID = mysql_fetch_array($client_ID);
$client_ID = $client_ID['id'];
$uploadDir = 'uploads/';
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;
}
$hostname_conndb = "localhost";
$database_conndb = "uploads";
$username_conndb = "root";
$password_conndb = "";
$conndb = mysql_connect($hostname_conndb, $username_conndb, $password_conndb) or trigger_error(mysql_error(),E_USER_ERROR);
if(!get_magic_quotes_gpc())
{
$fileName = addslashes($fileName);
$filePath = addslashes($filePath);
}
$date = date('Y-m-d H:i:s');
$sql = "INSERT INTO upload2 (name, client, size, type, path, date) ".
"VALUES ('$fileName', '$client_ID', '$fileSize', '$fileType', '$filePath', '$date')";
mysql_select_db($database_conndb, $conndb);
$result = mysql_query($sql, $conndb) or die(mysql_error());
echo "<br>File $fileName uploaded<br>";
}
?>
<html>
<head>
<title>Download File From MySQL</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<link href="styles.css" rel="stylesheet" type="text/css">
</head>
<body>
<?php
$hostname_conndb = "localhost";
$database_conndb = "uploads";
$username_conndb = "root";
$password_conndb = "";
$conndb = mysql_connect($hostname_conndb, $username_conndb, $password_conndb) or trigger_error(mysql_error(),E_USER_ERROR);
$sql = "SELECT * FROM upload2 WHERE client='".$client_ID."' ORDER BY date DESC";
mysql_select_db($database_conndb, $conndb);
$result = mysql_query($sql, $conndb) or die(mysql_error());
$rows = mysql_fetch_assoc($result);
$total_rows = mysql_num_rows($result);
?>
Welcome <?php echo $_SESSION['user_name'];?>
<form method="post" enctype="multipart/form-data">
<table width="350" border="0" cellpadding="1" cellspacing="1" class="box">
<tr>
<td width="246">
<input type="hidden" name="MAX_FILE_SIZE" value="2000000">
<input name="userfile" type="file" id="userfile">
</td>
<td width="80"><input name="upload" type="submit" class="box" id="upload" value=" Upload "></td>
</tr>
</table>
</form>
<?php if($total_rows > 0) { ?>
<table border="0" cellpadding="0" cellspacing="0" id="tbl_repeat">
<tr>
<th scope="col">FIle/Image Name</th>
<th scope="col" style="width:15%">Date</th>
<th scope="col" style="width:10%">Size</th>
<th scope="col" style="width:10%">Download</th>
</tr>
<?php do { ?>
<tr>
<td><?php echo $rows['name']; ?></td>
<td><?php echo $rows['date']; ?></td>
<td><?php echo $rows['size']; ?></td>
<td><a href="downloads.php?id=<?php echo $rows['id']; ?>">Download</a></td>
</tr>
<?php } while($rows = mysql_fetch_assoc($result)); ?>
</table>
<?php } else { echo "<p class="warn">Sorry there are no records available.</p>"; } ?>
<p><br />
<a href="logout.php">Logout </a></p>
</body>
</html>
Ce code fonctionne très bien. Le code de téléchargement est: downloads.php
<?php
require_once 'dbc.php';
page_protect();
$client_ID = mysql_query("SELECT id
FROM clients WHERE user_name='".$_SESSION['user_name']."'")or die(mysql_error());
$client_ID = mysql_fetch_array($client_ID);
$client_ID = $client_ID['id'];
$uploadDir = 'uploads/';
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;
}
$hostname_conndb = "localhost";
$database_conndb = "uploads";
$username_conndb = "root";
$password_conndb = "qaasim11";
$conndb = mysql_connect($hostname_conndb, $username_conndb, $password_conndb) or trigger_error(mysql_error(),E_USER_ERROR);
if(!get_magic_quotes_gpc())
{
$fileName = addslashes($fileName);
$filePath = addslashes($filePath);
}
$date = date('Y-m-d H:i:s');
$sql = "INSERT INTO upload2 (name, client, size, type, path, date) ".
"VALUES ('$fileName', '$client_ID', '$fileSize', '$fileType', '$filePath', '$date')";
mysql_select_db($database_conndb, $conndb);
$result = mysql_query($sql, $conndb) or die(mysql_error());
echo "<br>File $fileName uploaded<br>";
}
?>
<html>
<head>
<title>Download File From MySQL</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<link href="styles.css" rel="stylesheet" type="text/css">
</head>
<body>
<?php
$hostname_conndb = "localhost";
$database_conndb = "uploads";
$username_conndb = "root";
$password_conndb = "";
$conndb = mysql_connect($hostname_conndb, $username_conndb, $password_conndb) or trigger_error(mysql_error(),E_USER_ERROR);
$sql = "SELECT * FROM upload2 WHERE client='".$client_ID."' ORDER BY date DESC";
mysql_select_db($database_conndb, $conndb);
$result = mysql_query($sql, $conndb) or die(mysql_error());
$rows = mysql_fetch_assoc($result);
$total_rows = mysql_num_rows($result);
?>
Welcome <?php echo $_SESSION['user_name'];?>
<form method="post" enctype="multipart/form-data">
<table width="350" border="0" cellpadding="1" cellspacing="1" class="box">
<tr>
<td width="246">
<input type="hidden" name="MAX_FILE_SIZE" value="2000000">
<input name="userfile" type="file" id="userfile">
</td>
<td width="80"><input name="upload" type="submit" class="box" id="upload" value=" Upload "></td>
</tr>
</table>
</form>
<?php if($total_rows > 0) { ?>
<table border="0" cellpadding="0" cellspacing="0" id="tbl_repeat">
<tr>
<th scope="col">FIle/Image Name</th>
<th scope="col" style="width:15%">Date</th>
<th scope="col" style="width:10%">Size</th>
<th scope="col" style="width:10%">Download</th>
</tr>
<?php do { ?>
<tr>
<td><?php echo $rows['name']; ?></td>
<td><?php echo $rows['date']; ?></td>
<td><?php echo $rows['size']; ?></td>
<td><a href="downloads.php?id=<?php echo $rows['id']; ?>">Download</a></td>
</tr>
<?php } while($rows = mysql_fetch_assoc($result)); ?>
</table>
<?php } else { echo "<p class="warn">Sorry there are no records available.</p>"; } ?>
<p><br />
<a href="logout.php">Logout </a></p>
</body>
</html>
également ce code pour ma base de données:
CREATE TABLE IF NOT EXISTS `upload2` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`client` int(11) NOT NULL,
`name` varchar(30) NOT NULL,
`type` varchar(30) NOT NULL,
`size` int(11) NOT NULL,
`path` varchar(60) NOT NULL,
`date` datetime NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=MyISAM DEFAULT CHARSET=latin1 AUTO_INCREMENT=10 ;
--
-- Dumping data for table `upload2`
--
INSERT INTO `upload2` (`id`, `client`, `name`, `type`, `size`, `path`, `date`) VALUES
(1, 1, 'back.gif', 'image/gif', 1997, 'uploads/back.gif', '2010-09-19 12:17:05');
Quand je clique sur le lien de téléchargement dans upload.php je reçois l'erreur suivante Warning: mysql_fetch_array(): l'argument fourni n'est pas une ressource de résultat MySQL valide dans downloads.php sur la ligne 17 le fichier n'existe pas dans
Je ne suis pas sûr que le code fonctionne pour le téléchargement fichiers/images si cette erreur n'était pas leur que je ne peux pas comprendre comment réparer ce pronlem.
J'espère que vous réaliserez que vous devrez changer votre mot de passe DB après cela. –
Vous avez posté deux fois votre formulaire principal au lieu du code download.php. –