2015-01-05 57 views
0

我希望在上传前重新映像重命名,并且应该使用PHP将重命名的图像名称插入到数据库中。我正在使用以下代码上传图像并将其他一些数据插入到数据库中。请帮助我重新命名图片,因为我初学PHP。在上传前重命名图像,并在PHP数据库中插入名称

<?php 

    if(isset($_POST['submit'])) { 
    $post_image = $_FILES['post_image']['name']; 
    $post_image_tmp = $_FILES['post_image']['tmp_name']; 
    $post_content = $_POST['post_content']; 
    if ($post_image=='' OR $post_content=='') { 
     echo "<script>alert('Fiil In All Fields')</script>"; 
    } else { 
     move_uploaded_file($post_image_tmp, "../post_imgs/$post_image"); 
     $insert_post ="INSERT INTO posts(post_image,post_content)values('$post_image','$post_content')"; 
     $run_post= mysql_query($insert_post); 
     echo "<script>alert('Post has been Published Now...')</script>"; 
     echo "<script>window.open('index.php?insert_post=insert','_self')</script>"; 
    } 
    } 

?> 
+1

请在每一行的前面使用4位编辑代码。这种形式是不可读的。 –

回答

3
 $tmp_file = $_FILES['uploadedfile']['tmp_name']; 
     $ext = pathinfo($_FILES["uploadedfile"]["name"], PATHINFO_EXTENSION); 
     $rand = md5(uniqid().rand()); 
     $post_image = $rand.".".$ext; 
     move_uploaded_file($tmp_file,"../post_imgs/".$post_image); 
3

试试这个..

<?php 

    if(isset($_POST['submit'])) { 
    $post_image = $_FILES['post_image']['name']; 
    $post_image_tmp = $_FILES['post_image']['tmp_name']; 
    $post_content = $_POST['post_content']; 
    if ($post_image=='' OR $post_content=='') { 
     echo "<script>alert('Fiil In All Fields')</script>"; 
    } else { 
    $newname="image".$post_image; 
    $path='../post_imgs/'; 
$pathAndName = $path.$newname; 
$moveResult = move_uploaded_file($post_image_tmp, $pathAndName);//move to folder 
$insert_post ="INSERT INTO posts(post_image,post_content)values('$newname','$post_content')"; 
     $run_post= mysql_query($insert_post); 
     echo "<script>alert('Post has been Published Now...')</script>"; 
     echo "<script>window.open('index.php?insert_post=insert','_self')</script>"; 
    } 
    } 

?> 
+0

先生请请给我答案与我的代码...谢谢 – Shaggy

+0

@Shaggy我已更新我的答案与您的代码.check它,让我知道 –

相关问题