PDA

View Full Version : سوال: مشکل با اپلود فایل در PHP



amin7x
جمعه 13 اردیبهشت 1392, 19:17 عصر
سلام دوستان
من یک مشکلی دارم ، توی این کد من فایل اپلود نمیشه و اسم اون توی DB ذخیره نمیشه.
ولی بقیه مقادیر ذخیره میشه.



<form action="" method="post" name="Addnews" enctype="multipart/form-data">
<P>Title News :</P>
<input type="text" name="TitleNews" />
<P>Contact News :</P>
<textarea name="ContactNews" cols="30" rows="6"></textarea>
<p>Upload Image : (Max Upload 2MB , format .gif &amp; .jpeg)</p>
<input name="file" type="file" />
<br />
<input type="submit" value="Insert News" name="InsertNews" />
</form>


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

$TitleNews = mysql_real_escape_string(htmlspecialchars($_POST['TitleNews']));
$ContactNews = mysql_real_escape_string(htmlspecialchars($_POST['ContactNews']));

if(!empty($TitleNews) && !empty($ContactNews)){

if(!empty($_POST['file'])){

if ((($_FILES["file"]["type"] == "image/gif") || ($_FILES["file"]["type"] == "image/jpeg") || ($_FILES["file"]["type"] == "image/pjpeg")) && ($_FILES["file"]["size"] < 2000000))
{
if ($_FILES["file"]["error"] > 0)
{
$Error = "Return Code: " . $_FILES["file"]["error"] . "<br />";
}
else
{
if (file_exists("../images/Upload/" . $_FILES["file"]["name"]))
{
$FileName = $_FILES["file"]["name"].rand();
move_uploaded_file($_FILES["file"]["tmp_name"],"../images/Upload/" . $FileName);
$Accepti = "And imgaes is uploaded";
}
else
{
$FileName = $_FILES["file"]["name"];
move_uploaded_file($_FILES["file"]["tmp_name"],"../images/Upload/" . $FileName);
$Accepti = "And imgaes is uploaded";
}
}
}
else
{
$FileInvalid = "Invalid file";
}
}
if(isset($FileName)) {$File = $FileName;}
if(mysql_query("INSERT INTO `news` (`titlenews`, `contentnews`, `img`) VALUES ('{$TitleNews}', '{$ContactNews}', '{$File}')",$ConnectionVar))
{
$Accept = "News was shear";
}
}
else{
$EmptyFild = "please type all fild";
}
}
?>

ممنون مشم کمکم کنید.
این اروری که میده :


Notice: Undefined variable: File in ... On line 43

amin7x
شنبه 14 اردیبهشت 1392, 18:06 عصر
ببخشید دوستان اگه واجب نبود آپ نمیکردم.

omid_student
یک شنبه 15 اردیبهشت 1392, 11:35 صبح
ولی شما داخل کوئری نوشتین که متغییر $File رو ذخیره کنه در صورتی که اسم متغییر $Filename هست درسته؟

amin7x
یک شنبه 15 اردیبهشت 1392, 12:13 عصر
ولی شما داخل کوئری نوشتین که متغییر $File رو ذخیره کنه در صورتی که اسم متغییر $Filename هست درسته؟

درسته ولی توی این خط بهش گفتم که اگه $Filename وجود داشت بریزتش توی $File .


if(isset($FileName)) {$File = $FileName;}

amin7x
دوشنبه 16 اردیبهشت 1392, 22:04 عصر
کسی نمیتونه کد من رو درست کنه؟

Javidhb
دوشنبه 16 اردیبهشت 1392, 22:18 عصر
$File = '';
if(isset($FileName)) {$File = $FileName;}

amin7x
سه شنبه 17 اردیبهشت 1392, 20:14 عصر
$File = '';
if(isset($FileName)) {$File = $FileName;}


فایل آپلود نمیشه!!

MMSHFE
سه شنبه 17 اردیبهشت 1392, 21:57 عصر
این کد رو تست کنید:


if(isset($_POST['InsertNews'], $_POST['TitleNews'], $_POST['ContactNews'])) {
$TitleNews = mysql_real_escape_string($_POST['TitleNews']);
$ContactNews = mysql_real_escape_string($_POST['ContactNews']);
$FileName = '';
if(!empty($TitleNews) && !empty($ContactNews)) {
if(isset($_FILES['file']) && !empty($_FILE['file']['name'])) {
$file = &$_FILES['file'];
if(in_array($file['type'], array('image/gif', 'image/jpeg', 'image/pjpeg', 'image/png')) && $file['size'] < 2097152) {
if ($file['error'] > 0) {
$Error = '<p>Return Code: ' . $file['error'] . '</p>';
}
else {
$FileName = '../images/Upload/' . $file['name'];
if(file_exists($FileName)) {
do {
$FileName = '../images/Upload' . rand() . $file['name'];
} while(file_exists($name);
}
move_uploaded_file($file['tmp_name'], $FileName);
echo '<p>Image uploaded.</p>' . PHP_EOL;
}
}
}
else {
echo '<p>Invalid file.</p>' . PHP_EOL;
}
}
if(mysql_query("INSERT INTO `news` (`titlenews`,`contentnews`,`img`) VALUES ('{$TitleNews}','{$ContactNews}','{$FileName}')", $ConnectionVar)) {
echo '<p>News was shared.</p>' . PHP_EOL;
}
}
else {
echo '<p>Please fill all fields.</p>' . PHP_EOL;
}

amin7x
چهارشنبه 18 اردیبهشت 1392, 11:51 صبح
سلام خدمت استاد گرامی آقای شهرکی
کدتون یکم مشکل داشت درستش کردم ولی باز فایل آپلود نمیکنه ، چه قبل از اصلاح و چه بعدش:



if(isset($_POST['InsertNews'])){
if(isset($_POST['TitleNews'], $_POST['ContactNews'])) {
$TitleNews = mysql_real_escape_string($_POST['TitleNews']);
$ContactNews = mysql_real_escape_string($_POST['ContactNews']);
$FileName = '';
if(!empty($TitleNews) && !empty($ContactNews)) {
if(isset($_FILES['file']) && !empty($_FILE['file']['name'])) {
$file = &$_FILES['file'];
if(in_array($file['type'], array('image/gif', 'image/jpeg', 'image/pjpeg', 'image/png')) && $file['size'] < 2097152) {
if ($file['error'] > 0) {
$Error = '<p>Return Code: ' . $file['error'] . '</p>';
}
else {
$FileName = '../images/Upload/' . $file['name'];
if(file_exists($FileName)) {
do {
$FileName = '../images/Upload' . rand() . $file['name'];
} while(file_exists($name));
}
move_uploaded_file($file['tmp_name'], $FileName);
$ImageUpload = 'Image uploaded.' . PHP_EOL;
}
}
}
else {
$FileInvalid = 'Invalid file.' . PHP_EOL;
}

if(mysql_query("INSERT INTO `news` (`titlenews`,`contentnews`,`img`) VALUES ('{$TitleNews}','{$ContactNews}','{$FileName}')", $ConnectionVar)) {
$Accept = 'News was shared.' . PHP_EOL;
}
}
else {
$EmptyFild = 'Please fill all fields.' . PHP_EOL;
}
}
}